检查对象数组是否具有> 1属性值!== undefined

时间:2016-06-01 21:17:13

标签: javascript arrays object

我有一个像这样的对象数组:

var array = [
  {"name": "one", "value": .33},
  {"name": "one", "value": .54},
  {"name": "one", "value": undefined},

  {"name": "two", "value": .3},
  {"name": "two", "value": undefined},
  {"name": "two", "value": undefined},

  {"name": "three", "value": undefined},
  {"name": "three", "value": undefined},
  {"name": "three", "value": undefined},
];

我需要能够看到是否有任何唯一名称(一个/两个/三个)中只有一个数字" s" value"属性。所以,在这个例子中,答案是Yes,因为" two"的值属性。是:.3,未定义,未定义。

我确实有一个很好的方法来获得独特的名字"字段到数组:

function Names(a) {
  var temp = {};
  for (var i = 0; i < a.length; i++)
    temp[a[i].name] = true;
  var r = [];
  for (var k in temp)
    r.push(k);
  return r;
}

nameArray = Names(array);

但是当我开始寻找实际的循环时,我开始感到困惑。只是把它写出来,我认为它会像:

var count = 0;
for (objects with name == i){
  if (isNaN(value) == false){
    count++
    if(count > 1) {
      return true;
    }
  }
}

当然,这是伪代码,甚至可能不是正确的方向。任何帮助表示赞赏,感谢阅读!

3 个答案:

答案 0 :(得分:3)

您可以创建具有每个唯一名称的数字值的对象,然后使用/search检查任何名称的计数是否为 1 ,并且将返回some

&#13;
&#13;
true/false
&#13;
&#13;
&#13;

答案 1 :(得分:3)

如果你不能保证它们会按照阵列中的名字排序,那么Nenad有最好的答案。

但是,如果订单得到保证,您可以编写如下函数:

&#13;
&#13;
//Should be false
var array1 = [{"name":"one","value":0.33}];
//Should be false
var array2 = [{"name":"one","value":0.33},{"name":"one","value":0.54}];
//Should be false
var array3 = [{"name":"one","value":0.33},{"name":"one","value":0.54},{"name":"one","value":undefined}];
//Should be true - middle sequence contains one value
var array4 = [{"name":"one","value":0.33},{"name":"one","value":0.54},{"name":"one"},{"name":"two"},{"name":"two","value":0.3},{"name":"two"},{"name":"three"},{"name":"three"},{"name":"three"}];
//Should be true - last sequence contains one value
var array5 = [{"name":"one","value":0.33},{"name":"one","value":0.54},{"name":"one"},{"name":"two"},{"name":"two","value":0.3},{"name":"two", "value":0.3},{"name":"three"},{"name":"three"},{"name":"three","value":0.3}];

function containsUniqueValueForUniqueName(entry, index) {
  //So you can see in console how it short circuits the loop.
  console.log("Checking entry", entry);
  
  //If the value is a number, then increase the last count.
  if (!isNaN(entry.value)) {
    this.lastName = entry.name;
    this.lastCount += 1;
  }
  
  //When the lastName changes, or if its the last entry in the array, check if lastCount was 1. If yes, then exit true.
  if (this.lastName !== entry.name || index === array1.length - 1) {
    if (this.lastCount === 1)
      return true;
  
    //If its not 1, then we need to reset the count to 0.
    this.lastCount = 0;
  }
  
  return false;
}

var array1Result = array1.some(containsUniqueValueForUniqueName, {});
var array2Result = array2.some(containsUniqueValueForUniqueName, {});
var array3Result = array3.some(containsUniqueValueForUniqueName, {});
var array4Result = array4.some(containsUniqueValueForUniqueName, {});
var array5Result = array5.some(containsUniqueValueForUniqueName, {});

console.log('array1', array1Result);
console.log('array2', array2Result);
console.log('array3', array3Result);
console.log('array4', array4Result);
console.log('array5', array5Result);
&#13;
&#13;
&#13;

一旦遇到一个只有1个值的连续集,就会退出循环。

在一个非常大的数组上,可以为您节省相当多的迭代次数。同样,它要求您的数据按名称预先排序。所以,如果你从网络服务或类似的东西中得到它,那么我建议使用这个答案。

答案 2 :(得分:0)

如果您想考虑使用lodash(此处为jsbin example):

var array = [
  {"name": "one", "value": 0.33},
  {"name": "one", "value": 0.54},
  {"name": "one", "value": undefined},

  {"name": "two", "value": 0.3},
  {"name": "two", "value": undefined},
  {"name": "two", "value": undefined},

  {"name": "three", "value": undefined},
  {"name": "three", "value": undefined},
  {"name": "three", "value": undefined},
];

// groups objects by the key `name`
array = _.groupBy(array, function(obj) {
  return obj.name;
});

// gets the counts for each `name` of `value` properties that are numbers
array = _.mapValues(array, function(arr) {
  return arr.filter(function(obj) {
     return _.isNumber(obj.value);
  }).length;
});

console.log(array);

这将输出:

{
  one: 2,
  three: 0,
  two: 1
}

这告诉您名称为one的对象有两个value个数字,three有0个value个数字,two有一个value是一个数字。