1

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 ?

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

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.