I have an array of objects
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"
}
]
I want to filter that array of objects against another object
var myObj = {
"upper": true,
"three": true
}
so now myObj has a key "upper" and "three" and their values are true. So based these values I want to make a function to get all the objects in the data array that its key "body_focus" has value of "upper" and "difficulty" key that has a value of "three"
so the function should return only these objects
[
{
"body_focus": "upper",
"difficulty": "three",
"calories_min": "122",
"calories_max": "250"
},
{
"body_focus": "upper",
"difficulty": "three",
"calories_min": "150",
"calories_max": "280"
}
]
this is how I tried to approach the problem
var entry;
var found = [];
for (var i = 0; i < data.length; i++) {
entry = data[i];
for(var key in myObj) {
if(entry.body_focus.indexOf(key) !== -1) {
found.push(entry);
break;
}
}
}
my code above only checks for the key body_focus , so how can I check for both body_focus and difficulty ? it might seem silly but I've been stuck for hours and can't find a solution
{ "body_focus": "upper", "difficulty": "three" }?