0

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
asked May 1, 2019 at 23:43

3 Answers 3

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

what if the parameter can only be set as findOne(arr, query). How would you pass in the object? function findOne(arr, query) { }
Add this line as the first line in the function @Jim - const { id, squad } = query.
@Jim Edited my answer to display your needs.
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.
OK @Jim, I'll fix that.
|
0

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

0

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' }));

answered May 2, 2019 at 0:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.