kallada
kallada

Reputation: 1929

Iterating and filtering with ES6/lodash

We have an array of objects like:

const persons = [{
    name: "john",
    age: 23
}, {
    name: "lisa",
    age: 43
}, {
    name: "jim",
    age: 101
}, {
    name: "bob",
    age: 67
}];

And an array of a attribute values of the objects in object

const names = ["lisa", "bob"]

How can we find persons with name in the names array using es6, like:

const filteredPersons = [{
    name: "lisa",
    age: 43
}, {
    name: "bob",
    age: 67
}];

Upvotes: 4

Views: 1344

Answers (6)

Arman Charan
Arman Charan

Reputation: 5797

See Closures, Set, and Array.prototype.filter() for more info.

// Input.
const persons = [{name: "john",age: 23}, {name: "lisa",age: 43}, {name: "jim",age: 101}, {name: "bob",age: 67}]
const names = ["lisa", "bob"]

// Filter.
const filter = (A, B) => (s => A.filter(x => s.has(x.name)))(new Set(B))

// Output.
const output = filter(persons, names)

// Proof.
console.log(output)

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

I would suggest to use indexOf as includes does not work in IE browser. Also, using {name} works as Destructuring assignment that will hold the value of name property of the object.

const persons = [{
  name: "john",
  age: 23
}, {
  name: "lisa",
  age: 43
}, {
  name: "jim",
  age: 101
}, {
  name: "bob",
  age: 67
}];
const names = ["lisa", "bob"];

console.log(persons.filter(({name}) => names.indexOf(name) !== -1))

Upvotes: 3

31piy
31piy

Reputation: 23859

In case you want to perform it in n complexity, here is a way to do it:

  1. Create a map with key as person's name and value as person's object.
  2. Map the criteria array and extract the person objects from the map create in step 1.

Here is a working demo:

const persons = [{
  name: "john",
  age: 23
}, {
  name: "lisa",
  age: 43
}, {
  name: "jim",
  age: 101
}, {
  name: "bob",
  age: 67
}];

const names = ["lisa", "bob"];
const map = persons.reduce((acc, item) => {
  acc[item.name] = item;
  return acc;
}, {});
const result = names.map(name => map[name]);
console.log(result);

Note: This solution assumes that only unique person names are in the source array. It needs to be tweaked to handle duplicates.

Upvotes: 1

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

lodash

You can try following

const persons = [{name: "john", age: 23},
    {name: "lisa",age: 43}, 
    {name: "jim", age: 101}, 
    {name: "bob",age: 67}];


const names = ["lisa", "bob"]


const filteredPersons = _.filter(persons, function(person) {
    return _.indexOf(names, person.name) !== -1;
});

console.log(filteredPersons);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>

Upvotes: 1

Narendra Jadhav
Narendra Jadhav

Reputation: 10262

You can use filter() and inlcudes() to get required result.

DEMO

const persons = [{
  name: "john",
  age: 23
}, {
  name: "lisa",
  age: 43
}, {
  name: "jim",
  age: 101
}, {
  name: "bob",
  age: 67
}];
const names = ["lisa", "bob"];

console.log(persons.filter(({
  name
}) => names.includes(name)))
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 3

Suren Srapyan
Suren Srapyan

Reputation: 68665

ES6

Use filter function with predicate and in it check the existence of the name in the names array.

const persons = [
    {name: "john", age:23},
    {name: "lisa", age:43},
    {name: "jim", age:101},
    {name: "bob", age:67}
];

const names = ["lisa", "bob"];

const filtered = persons.filter(person => names.includes(person.name));

console.log(filtered);

Upvotes: 4

Related Questions