0

I have the following array

const accounts =[{'accounts':{id:1,name: 'Jill','occupation': 'Welder'}, {id:2,name: 'John','occupation': 'Driver'}, {id:3,name: 'Steve','occupation': 'Accountants'}}, 
{'accounts':{id:1,name: 'James','occupation': 'Welder'},{id:1,name: 'Steve','occupation': 'Security'},{id:1,name: 'Sarah','occupation': 'Waiter'}}, 
{'accounts':{id:1,name: 'George','occupation': 'Developer'},{id:1,name: 'Andrew','occupation': 'Security'},{id:1,name: 'Frank','occupation': 'Welder'}}];

and I would like to be able to make a selection of like Welder or driver etc...

I have tried using this

const welders= accounts.filter(x => x.accounts.every(ls1=> ls1.occupation ==='Welder'));

but it returns an empty array.

Can someone suggest the best way to achieve it

2
  • What is the expected output? Commented Apr 25, 2021 at 17:07
  • try in chrome, no error just an empty array but I got an answer from @iamentafaz Commented Apr 25, 2021 at 17:13

1 Answer 1

2
const welders= accounts.map(x => x.accounts.filter(ls1=> ls1.occupation ==='Welder' || ls1.occupation ==='Driver'));
Sign up to request clarification or add additional context in comments.

2 Comments

cheers thanks that did it. Thanks a bunch. Will accept in 5
Could you please accept the answer @JackM?

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.