1

I have 2 arrays like this ...

var heroes = [{
    name: "Batman",
    franchise: "DC"
  },
  {
    name: "Ironman",
    franchise: "Marvel"
  },
  {
    name: "Thor",
    franchise: "Marvel"
  },
  {
    name: "Superman",
    franchise: "DC"
  }
];

var stars = ["Thor", "Superman"];

var marvelHeroes = heroes.filter(function(hero) {
  return stars.indexOf(hero.name) >= 0;
});

console.log(marvelHeroes);

This code is returning 'undefined'. So essentially I am trying to filter an array against values in another array.

Can someone please advise what am I doing wrong ? Thanks

My expected output is an array of objects like the following ...

[
{name: "Thor", franchise: "Marvel"},
{name: "Superman", franchise: "DC"}
];

I have seen a similar question here, but that solution is not working for me ...

2
  • Which hero are you trying to select? Select with hero[index].name Commented Jun 10, 2018 at 13:46
  • I converted your code to a snippet and logged the object to the console. It works as expected. Commented Jun 10, 2018 at 13:55

2 Answers 2

1

It should be just return stars.indexOf(item.name) !== -1 ;

DEMO

var heroes = [
    {name: "Batman", franchise: "DC"},
    {name: "Ironman", franchise: "Marvel"},
    {name: "Thor", franchise: "Marvel"},
    {name: "Superman", franchise: "DC"}
];

var stars = ["Thor","Superman"];

var filtered = heroes.filter(function(item) {
        return stars .indexOf(item.name) !== -1 ;
});

console.log(filtered);

Sign up to request clarification or add additional context in comments.

7 Comments

>= 0 is > -1.
Because the OP's code is working correctly. Please see the snippet. This question should be closed as off-topic.
but he has mentioned it returns undefined
I understand, but for clarification, a comment would have sufficed.
Same question, I am not sure why @31piy down-voted my question as well ? People ask questions, because they are not sure about their code. This is wrong use of a down-vote.
|
0

You can use includes to determine whether an array includes a certain element

var heroes = [{name:"Batman",franchise:"DC"},{name:"Ironman",franchise:"Marvel"},{name:"Thor",franchise:"Marvel"},{name:"Superman",franchise:"DC"}];
var stars = ["Thor", "Superman"];

var marvelHeroes = heroes.filter(function(hero) {
  return stars.includes(hero.name);
});

console.log(marvelHeroes);

For shorter code:

var heroes = [{name:"Batman",franchise:"DC"},{name:"Ironman",franchise:"Marvel"},{name:"Thor",franchise:"Marvel"},{name:"Superman",franchise:"DC"}]
var stars = ["Thor", "Superman"];

var marvelHeroes = heroes.filter(o => stars.includes(o.name));

console.log( marvelHeroes );

Doc: includes()

3 Comments

Thanks @Eddie, From a performance point of view, which one is faster ? 'indexof' or 'includes'. My array is huge so performance is important.
I like using includes() because it clearer Im not sure about the performance though.
Hey @Eddie, I didn't down-voted, I think its 31piy, who down-voted my question initially, as well as all the answers. See the conversation in Sajeetharan's reply above.

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.