I have the following array:
const HEROES = [
{ id: 1, name: 'Captain America', squad: 'Avengers' },
{ id: 2, name: 'Iron Man', squad: 'Avengers' },
{ id: 3, name: 'Spiderman', squad: 'Avengers' },
{ id: 4, name: 'Superman', squad: 'Justice League' },
{ id: 5, name: 'Wonder Woman', squad: 'Justice League' },
{ id: 6, name: 'Aquaman', squad: 'Justice League' },
{ id: 7, name: 'Hulk', squad: 'Avengers' },
];
I'm trying to pass another object { id: 5, squad: 'Justice League' } into the array to find the matching object.
for example:
findOne(HEROES, { id: 5, squad: 'Justice League' })
should return
{ id: 5, name: 'Wonder Woman', squad: 'Justice League' }
I'm not sure how to start this, any help would be appreciated.
Jack Bashford
44.3k11 gold badges56 silver badges83 bronze badges
3 Answers 3
Use find
:
const HEROES = [
{ id: 1, name: 'Captain America', squad: 'Avengers' },
{ id: 2, name: 'Iron Man', squad: 'Avengers' },
{ id: 3, name: 'Spiderman', squad: 'Avengers' },
{ id: 4, name: 'Superman', squad: 'Justice League' },
{ id: 5, name: 'Wonder Woman', squad: 'Justice League' },
{ id: 6, name: 'Aquaman', squad: 'Justice League' },
{ id: 7, name: 'Hulk', squad: 'Avengers' },
];
const findOne = (arr, query) => {
const { id, squad } = query;
return arr.find(({ id: a, squad: b }) => (id != undefined ? a == id : true) && (b != undefined ? b == squad : true));
};
console.log(findOne(HEROES, { id: 5, squad: "Justice League" }));
answered May 1, 2019 at 23:53
6 Comments
Jim
what if the parameter can only be set as findOne(arr, query). How would you pass in the object? function findOne(arr, query) { }
Jack Bashford
Add this line as the first line in the function @Jim -
const { id, squad } = query
.Jack Bashford
@Jim Edited my answer to display your needs.
Jim
Thank you for the quick response. I guess I should have made this more clear, the 'query' parameter is not exactly set to { id: 5, squad: "Justice League" }. This method would work in this instance but if I change the object to just {id: 3} or {name: 'Hulk'} it would fail.
Jack Bashford
OK @Jim, I'll fix that.
|
Have a look at a utility library like underscore JS or Lodash. They have just such functionality for that.
From lodash docs:
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }
];
_.find(users, function(o) { return o.age < 40; });
// => object for 'barney'
// The `_.matches` iteratee shorthand.
_.find(users, { 'age': 1, 'active': true });
// => object for 'pebbles'
answered May 1, 2019 at 23:49
Comments
Similar Code using find
const HEROES = [
{ id: 1, name: 'Captain America', squad: 'Avengers' },
{ id: 2, name: 'Iron Man', squad: 'Avengers' },
{ id: 3, name: 'Spiderman', squad: 'Avengers' },
{ id: 4, name: 'Superman', squad: 'Justice League' },
{ id: 5, name: 'Wonder Woman', squad: 'Justice League' },
{ id: 6, name: 'Aquaman', squad: 'Justice League' },
{ id: 7, name: 'Hulk', squad: 'Avengers' },
];
const findOne=(arr,obj)=>arr.find(x=>x.id==obj.id&&x.squad==obj.squad);
console.log(findOne(HEROES, { id: 7, squad: 'Avengers' }));
Comments
Explore related questions
See similar questions with these tags.
lang-js