Quentin Sutkowski
Quentin Sutkowski

Reputation: 23

Get specific element on array of array JS without index equivalent with id (react)

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

Answers (1)

Aziz.G
Aziz.G

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

Related Questions