最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why doesn't my filter function work? (Javascript). - Stack Overflow

programmeradmin6浏览0评论

What I am trying to acplish, being able to use the filter function to filter out the people that have the age: 21 and the color: blue.

Any suggestions on how to make it work? Thanks in advance.

Here is my code:

var arr = [   { name: 'Steve', age: 18, color: 'red' },   { name: 'Louis', age: 21, color: 'blue' },   { name: 'Mike', age: 20, color: 'green' },   { name: 'Greg', age: 21, color: 'blue' },   { name: 'Josh', age: 18, color: 'red' } ];

var filter = function(arr, criteria){
    var filtered = []
    for(var i=0; i<arr.length; i++){
        if (arr[i] === criteria) {
            filtered.push(arr[i])
        }
        return filtered; 
    }
     }

console.log(filter(arr, { age: 21, color: 'blue' }));

What I am trying to acplish, being able to use the filter function to filter out the people that have the age: 21 and the color: blue.

Any suggestions on how to make it work? Thanks in advance.

Here is my code:

var arr = [   { name: 'Steve', age: 18, color: 'red' },   { name: 'Louis', age: 21, color: 'blue' },   { name: 'Mike', age: 20, color: 'green' },   { name: 'Greg', age: 21, color: 'blue' },   { name: 'Josh', age: 18, color: 'red' } ];

var filter = function(arr, criteria){
    var filtered = []
    for(var i=0; i<arr.length; i++){
        if (arr[i] === criteria) {
            filtered.push(arr[i])
        }
        return filtered; 
    }
     }

console.log(filter(arr, { age: 21, color: 'blue' }));
Share Improve this question asked Dec 16, 2015 at 2:47 user3522720user3522720 631 silver badge6 bronze badges 1
  • Because === pares object identity, instead of telling you wether the lhs has all the properties that the rhs has. – Bergi Commented Dec 16, 2015 at 2:56
Add a ment  | 

4 Answers 4

Reset to default 3

Cant pare object with ===

Testing objects equality in NodeJS

You should add return outside for-loop

var filter = function(arr, criteria){
    return arr.filter( function(item){
        return Object.keys(criteria).every(function(key) {
            return item[key] == criteria[key];
        });
    });
}

The reason your solution is not working is because you cannot pare two objects using the equality operator, so you have to actually pare the properties of the objects, use a library like lodash or underscore with a built in parison functino, or write a deep equals function yourself that iterates through the properties of each and returns false if at any point they are not equal.

Here is one possible solution:

var filter = function(arr, criteria){
  var filtered = []
  arr.forEach(function (item) {
    if (item.age === criteria.age && item.color === criteria.color) {
      filtered.push(item);
    }
  });
  return filtered;
};

You'll have to loop over those Objects in your Array and test for their properties and property values. Here's a backward patible way to do it:

function hasCompObjPropsAndVals(obj, pObj){
  var l = 0, c = 0;
  for(var i in pObj){
    l++;
    for(var p in obj){
      if(i === p && pObj[i] === obj[p]){
        c++;
        break;
      }
    }
  }
  if(l !== 0 && l === c){
    return true;
  }
  return false;
}
function filter(objArray, pObj){
  var res = [];
  for(var i=0,l=objArray.length; i<l; i++){
    var o = objArray[i];
    if(hasCompObjPropsAndVals(o, pObj)){
      res.push(o);
    }
  }
  return res;
}
var arr = [   { name: 'Steve', age: 18, color: 'red' },   { name: 'Louis', age: 21, color: 'blue' },   { name: 'Mike', age: 21, color: 'green' },   { name: 'Greg', age: 21, color: 'blue' },   { name: 'Josh', age: 18, color: 'red' } ];
console.log(filter(arr, {age: 21, color: 'blue'}));
发布评论

评论列表(0)

  1. 暂无评论