Reputation: 22530
I have an object that contains an array called applicants:
const persons={"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}}
How can I return the item from the entities.applicants array by id?
I tried something like:
const applicantsById = persons.entities.applicants.find(a => a.id ===id)
console.log(applicantsById.get('16671520041'))
But I can't figure out how to pass in the id?
Upvotes: 1
Views: 1939
Reputation: 50684
Using vanilla javascript:
You need to define what id
is before you set your applicantsById
variable. This way you can find an id
in your array of objects which equals the id
you defined.
const id = '16671520041';
const applicantsById = persons.entities.applicants.find(a => a.id === id);
You can extend this to a function if you wish to use this with multiple ids:
const persons = {"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}};
const getApplicantById = _id => persons.entities.applicants.find(({id}) => id === _id);
console.log(getApplicantById('16671520041')); // Abouli obj
console.log(getApplicantById('16671520039')); // Purdy obj
Or, using Lodash and ES6:
const persons = {"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}};
const id = "16671520041";
const res = _.find(persons.entities.applicants, {id});
console.log(res); // Abouli obj
<script src="https://cdn.jsdelivr.net/lodash/4.16.4/lodash.min.js"></script>
Upvotes: 2
Reputation: 12152
Use filter for this
const persons={"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}}
console.log(get(16671520039));
function get(id)
{
return persons.entities.applicants.filter((e)=>e.id===id)
}
Upvotes: 0
Reputation: 63524
You can either define your id before you do your find
:
const id = '16671520041';
const applicantsById = persons.entities.applicants.find(a => a.id ===id)
Or you can define a function that accepts an id as an argument. The other answers have covered how to do this with modern ES6 arrow functions. If you're unfamiliar with them here's the ES5 function declaration-way of writing it. Here I've also passed in the persons data to the function.
const persons={"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}}
// Accept persons data, and an id
function getApplicantById(persons, id) {
// Return the found applicant from the function
return persons.entities.applicants.find(a => a.id ===id);
}
// Pass in the data, and the id to the function
const applicant = getApplicantById(persons, '16671520041');
console.log(applicant);
Upvotes: 0
Reputation: 11
You should create a function.
const applicantsById = (id) => persons.entities.applicants.find(a => a.id === id)
applicantsById('16671520041')
this will return you required result
Upvotes: 0
Reputation: 8632
applicantsById
needs to be a function parametrized on the id
const persons={"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}}
const applicantsById = id => persons.entities.applicants.find(a => a.id ===id)
console.log(applicantsById('16671520041'))
Upvotes: 2