Skip to content Skip to sidebar Skip to footer

Filter Array Of Objects Against Another Object In Javascript

I have an array of objects var data = [ { 'body_focus': 'upper', 'difficulty': 'three', 'calories_min': '122', 'calories_max': '250' }, {

Solution 1:

You could use an object for searching which gives the keys and the wanted values, like

search = {
    body_focus: "upper",
    difficulty: "three"
}

then iterate through the array and check all properties of search with the values of the actual object. Return true if all search criteria matches.

var data = [{ body_focus: "upper", difficulty: "three", calories_min: "122", calories_max: "250" }, { body_focus: "upper", difficulty: "three", calories_min: "150", calories_max: "280" }, { body_focus: "lower", difficulty: "two", calories_min: "100", calories_max: "180" }, { body_focus: "total", difficulty: "four", calories_min: "250", calories_max: "350" }],
    search = { body_focus: "upper", difficulty: "three" },
    result = data.filter(function (o) {
        returnObject.keys(search).every(function (k) {
            return o[k] === search[k];
        });
    });

console.log(result);
.as-console-wrapper { max-height: 100%!important; top: 0; }

Solution 2:

This should do the trick I think.

var myObj = {
    "upper": true,
    "three": true
}
var found = [];
for (var i = 0; i < data.length; i++) {
    entry = data[i];
    if(myObj[entry.body_focus] && myObj[entry.difficulty]) {
      found.push(entry);
    }
}

With myObj[entry.body_focus] you are checking if the myObj has the property upper and if it's true. Same with difficulty.

Solution 3:

This solution is using a combination of Array.prototype.filter and Array.prototype.every to match all the values in your data object with the keys in the param object.

var data = [
  {"body_focus": "upper", "difficulty": "three", "calories_min": "122", "calories_max": "250"}, 
  {"body_focus": "upper","difficulty": "three","calories_min": "150","calories_max": "280"},
  {"body_focus": "lower","difficulty": "two","calories_min": "100","calories_max": "180"}, 
  {"body_focus": "total","difficulty": "four","calories_min": "250","calories_max": "350"}
]

var params = {
  "upper": true,
  "three": true
}

functionfilter(params, data) {
  const keys = Object.keys(params)
  return data.filter(item =>
    keys.every(value => ~Object.values(item).indexOf(value))
  )
}


console.log(
  filter(params, data)
)
<scriptsrc="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

Solution 4:

Nina's answer should work for you. But in case you are interested, another way to do it:

var data = [{
  "body_focus": "upper",
  "difficulty": "three",
  "calories_min": "122",
  "calories_max": "250"
}, {
  "body_focus": "upper",
  "difficulty": "three",
  "calories_min": "150",
  "calories_max": "280"
}, {
  "body_focus": "lower",
  "difficulty": "two",
  "calories_min": "100",
  "calories_max": "180"
}, {
  "body_focus": "total",
  "difficulty": "four",
  "calories_min": "250",
  "calories_max": "350"
}];

var search = { 
  body_focus: "upper", difficulty: "three" 
};

var results=$.grep(data,function(datum,index){
  var sat=true;
  Object.keys(search).forEach(function(el){
    if(datum[el]!=search[el])
      {
        sat=false;
        returnfalse;
      }
  });
  return sat;
});
console.log(results);
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The point being you could use $.grep to write a custom filter function and check some condition using the first parameter 'datum' or the second 'index' and return true to indicate filter match or false to indicate otherwise

Solution 5:

it would be a good idea to change your myObj object's structure to be more of

let myObj = 
{
    body_focus: "upper",
    difficulty: "three"
}     
as your search object. (as it was mentioned in one of the answers)

you can then use filter on your original array of objects to get what you need.

let results = data.filter(item => item.body_focus === myObj.body_focus 
                               && item.difficulty === myObj.difficulty);

Post a Comment for "Filter Array Of Objects Against Another Object In Javascript"