3

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"
 }
]
2
  • 5
    return types.includes(item.type)?... developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… or, if you don't like includes, you can always use the fancy indexOf: return types.indexOf(item.type) > -1. Commented Sep 19, 2018 at 10:09
  • 1
    Minor thing to add, you could just return the expression return someObj.filter(...) instead of creating a variable and then returning it. Commented Sep 19, 2018 at 10:13

5 Answers 5

6

You could search for the type.

ES5 with Array#indexOf

return types.indexOf(item.type) !== -1;

ES6 with Array#includes

return types.includes(item.type);
Sign up to request clarification or add additional context in comments.

Comments

3

You can use Array#includes() to check the type:

let someObj = [
    {name: "Obj1",type: "a"},
    {name: "Obj2",type: "b"},
    {name: "Obj3",type: "c"}
]       

function filter(types) {
  return someObj.filter(function(item) {
    return types.includes(item.type)
  }); 
}

console.log(filter(["a", "c"]));

Comments

0

Or a long winded mildly old-fashioned way:

var someObj = [
    {
        name: "Obj1",
        type: "a"
    },
    {
        name: "Obj2",
        type: "b"
    },
    {
        name: "Obj3",
        type: "c"
    }
];


function filter(types) {
  var filtered = someObj.filter(function(item) {
  if ( types.indexOf(item.type) >= 0 )
    {
    	return item;
    }

  });       

  return filtered;  
}

var x = filter(["a", "c"]);
console.log(x);

Comments

0

If you can use ES6 syntax you could use the following:

var someObj = [
    {
        name: "Obj1",
        type: "a"
    },
    {
        name: "Obj2",
        type: "b"
    },
    {
        name: "Obj3",
        type: "c"
    }
]

function filter(types){
  return someObj.filter(obj => types.includes(obj.type))
}

console.log(filter(["a", "c"]))

Comments

-1

You can use a reduce function here:

function filter(someObj, types) {
    return someObj.reduce((acc, obj) => {
        return types.includes(obj.type)? [...acc, obj] : acc 
    }, [])
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.