0
var students = ["Malcom", "Jayne"];
var persons = [
    {firstname : "Malcom", lastname: "Reynolds"},
    {firstname : "Kaylee", lastname: "Frye"},
    {firstname : "Jayne", lastname: "Cobb"}
];

I want to filter(or map, forEach whatever) persons array with students array. Students array is dynamic, so its length can be changed.

Any ideas?

1
  • Can you be more specific about what is it that you are trying to achieve? Do you want to return each person that is a student? And do you want to result to be in another new array? Commented Sep 15, 2016 at 21:13

5 Answers 5

1

It can be done with filter along with some.

var students = ["Malcom", "Jayne"];

var persons = [
    {firstname : "Malcom", lastname: "Reynolds"},
    {firstname : "Kaylee", lastname: "Frye"},
    {firstname : "Jayne", lastname: "Cobb"}
];

console.log(persons.filter(person => students.some(student => student == person.firstname)))

// An advice, maybe you want to put everything to lower case
// to prevent a wrong result because of case mismatch
// (e.g. "Malcom" !== "malcom"). 
// It would be:

persons.filter(person => students.some(student => student.toLowerCase() === person.firstname.toLowerCase()))

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

Comments

0

You could use Set and Array#filter.

var students = ["Malcom", "Jayne"],
    persons = [{ firstname: "Malcom", lastname: "Reynolds" }, { firstname: "Kaylee", lastname: "Frye" }, { firstname: "Jayne", lastname: "Cobb" }],
    result = persons.filter(function (a) {
        return this.has(a.firstname);
    }, new Set(students));

console.log(result);

ES6

var students = ["Malcom", "Jayne"],
    persons = [{ firstname: "Malcom", lastname: "Reynolds" }, { firstname: "Kaylee", lastname: "Frye" }, { firstname: "Jayne", lastname: "Cobb" }],
    result = persons.filter((set => a => set.has(a.firstname))(new Set(students)));

console.log(result);

Comments

0

You should be able to filter the array by checking if their firstname is in the students array:

persons.filter(function(person) { return students.indexOf(person.firstname) >= 0 });

Comments

0

For this you can use filter() and includes()

var students = ["Malcom", "Jayne"];
var persons = [
    {firstname : "Malcom", lastname: "Reynolds"},
    {firstname : "Kaylee", lastname: "Frye"},
    {firstname : "Jayne", lastname: "Cobb"}
];

var r = persons.filter(function(o) {
  return students.includes(o.firstname);
});

console.log(r)

1 Comment

Is worth mentioning that Internet Explorer doesn't support includes
0
var students = ["Malcom", "Jayne"];
var persons = [
    {firstname : "Malcom", lastname: "Reynolds"},
    {firstname : "Kaylee", lastname: "Frye"},
    {firstname : "Jayne", lastname: "Cobb"}
];
var filteredPersons = persons.filter(person => students.indexOf(person.firstname) > -1);

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.