Reputation: 23
I have a little problem wih my react app.
I try to pick name element on an array like this, with only the value of id correspondant.
Ex of array :
[{"id":2,"name":"Jean","content":"hey brother"},{"id":6,"name":"Jack","content":"hey sister"}]
I have just id = 2. How to reach the name "Jean" simply ?
Upvotes: 1
Views: 185
Reputation: 3721
You can use find
+ object destructing
const per = [{
"id": 2,
"name": "Jean",
"content": "hey brother"
}, {
"id": 6,
"name": "Jack",
"content": "hey sister"
}]
const {
name
} = per.find(pers => pers.id === 2)
console.log(name)
Upvotes: 1