无法从Object.values

时间:2017-08-22 23:46:49

标签: javascript javascript-objects

当我从集合中创建一个值字符串时,不会执行值null。只有当它是对象集合[i]的值时,我才需要在我的字符串中附加null。或者至少为什么没有通过null将不胜感激。

function truthCheck(collection, pre) {
  // Is everyone being true?
  var arr = '';
  for ( i=0; i<collection.length; i++ ){
    if ( pre in collection[i]){
      arr += ' ' + Object.values(collection[i]);//Problem is here
    }
  }
  arr += null; //If I append it after..
  return arr; //This returns as I want it to. This return is for testing 
              //only.
  arr = arr.trim();
  var regex = /NaN|null|0|undefined|false/;
  arr = arr.split(" ");
  for ( i=0; i<arr.length; i++){
    if( arr[i].search(regex) != -1 ){
      arr.splice(i,1);
    }
  }
  if ( arr.length == collection.length ){
    return true;
  }else{
    return false;
  }
}

truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": 
true}, 
{"name": "FastFoward", "onBoat": null}], "onBoat");

1 个答案:

答案 0 :(得分:0)

基斯把它钉在头上。添加&amp;&amp; collection [i] [pre]将我的字符串缩减为正则表达式将正确过滤数组。

for ( i=0; i<collection.length; i++ ){
  if ( pre in collection[i] && collection[i][pre] ){
    arr += ' ' + Object.values(collection[i]);
  }
}