Let's say I have an array of objects like this:
someObj: [
{
name: "Obj1",
type: "a"
},
{
name: "Obj2",
type: "b"
},
{
name: "Obj3",
type: "c"
}
]
Now I have a function that should return an array of objects from the someObj array if the parameters passed have the same type property. The thing is the parameter passed is an array and I'm not sure how to make that comparison.
function filter(types) {
var filtered = someObj.filter(function(item) {
return item.type == ???
});
return filtered;
}
filter(["a", "c"]);
How do I compare each item in the array parameter that's passed to item.type so that if they're equal, then the filter function would return to me an array like so:
[
{
name: "Obj1",
type: "a"
},
{
name: "Obj3",
type: "c"
}
]
return types.includes(item.type)?... developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… or, if you don't likeincludes, you can always use the fancy indexOf:return types.indexOf(item.type) > -1.return someObj.filter(...)instead of creating a variable and then returning it.