如何检查JavaScript中的对象中是否存在值

时间:2017-02-13 17:57:16

标签: javascript angularjs object

我在角度工厂服务中有一个函数来获取一些数据。在使用对象之前,如何检查对象内是否存在某个值?

这是我一直在尝试的......

export function getRandomSuits(nrOfPlayers) {

    const rangeMin = 1;
    const rangeMax = 6;
    var tempNum = [];
    for(i = 0; i <= rangeMax - rangeMin; i++){
        tempNum[i] = rangeMin + i;
    }
    let nums = [];
    while (nums.length < nrOfPlayers) {
        let index = Math.floor(Math.random() * tempNum.length);
        var numberExists = _.indexOf(nums, tempNum[index]);
        if(numberExists < 0) {
            nums.push(tempNum[index]);
            tempNum.splice(tempNum, index));
        }
    }

    return nums;
}

所以我只想在categories.fetch = function(app){ if(!app.subject.name.length){ return false; } var p = Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()}); } 调用中使用它之前检查app.subject.name中是否有值...

由于

4 个答案:

答案 0 :(得分:2)

您的代码将检索length属性的值,并尝试将其转换为布尔值以用于if/then测试,但如果值恰好为null,则会抛出错误

此外,如果您的测试只是:app.subject.name,如果该值恰好是一个假值,如0false,则会得到误报,这两个值都是完美的有效值。

对于字符串,最简单的测试是检查非空字符串和非空字符串。如果该值是由最终用户提供的,则最好先在字符串上调用.trim()以删除可能无意中添加的任何前导或尾随空格。

var myObj = { 
  test : 0,
  testing : null
}


// This will fail with an error when the value is null
/*
if(myObj.testing.length){ 
  console.log("The testing property has a value."); 
} else {
  console.log("The testing property doesn't have a value."); 
}
*/

// This will return a false positive when the value is falsy
if(myObj.test){ 
  console.log("The test property has a value."); 
} else {
  console.log("The test property doesn't have a value."); // <-- Incorretly reports this
}

// This explicit test will pass and fail correctly
if(myObj.testing !== "" && myObj.testing !== null){ 
  console.log("The testing property has a value."); 
} else {
  console.log("The testing property doesn't have a value."); 
}

此外,如果值在那里,请将您的代码放在if的真分支中,不要担心return false

categories.fetch = function(app){
  if(app.subject.name !== "" && app.subject.name !== null) {
    var p = 
      Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()});
  }

答案 1 :(得分:1)

  

hasOwnProperty()方法返回一个布尔值,指示对象是否具有指定的属性。 MDN Docs

实施例

var priceOfFood = {
    pizza: 14,
    burger 10
}

priceOfFood.hasOwnProperty('pizza') // true
priceOfFood['pizza'] // 14
priceOfFood.hasOwnProperty('chips') // false
priceOfFood['chips'] // undefined

答案 2 :(得分:1)

我知道这个问题并没有问到Lodash,但是我设法用这种方式做了很多检查,它完美无瑕。在你的情况下,它将是这样的:

categories.fetch = function(app){
  if (_.isEmpty(app.subject.name)) {
   return false;
  }
 var p = Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()});
}

如果您希望在app对象中可能无法使用这些键,则可以这样做:

categories.fetch = function(app){
  if (_.isEmpty(_.get(app, "subject.name"))) {
   return false;
  }
 var p = Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()});
}

或者简单地说:

categories.fetch = function(app){
  if (!_.get(app, "subject.name")) {
   return false;
  }
 var p = Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()});
}

答案 3 :(得分:0)

简单:

if(!app.subject.name){
        return ;
    }
相关问题