0

In react app, I am trying to get single row from an array using id but it doesn't work.

const result = [
 {id: 0, important: false, name: "Shruthi R", phone: "9834233433", email: "[email protected]", work: "NicheSoft", city: "Mysore"},
 {id: 1, important: false, name: "Shivay", phone: "9442233166", email: "[email protected]", work: "MicroSoft", city: "Mumbai"},
 {id: 2, important: false, name: "Yash Sindhya", phone: "7455454354", email: "[email protected]", work: "AirVoice", city: "Pune"},
 {id: 3, important: false, name: "Rajat", phone: "8456546555", email: "[email protected]", work: "Airtel", city: "Delhi"},
 {id: 5, important: false, name: "Surya S", phone: "9956546546", email: "[email protected]", work: "Vodafone", city: "Chennai"},
 {id: 6, important: false, name: "Paridhi", phone: "8856544422", email: "[email protected]", work: "MicroTech", city: "Lucknow"},
];

My onEditClick function is as shown below, here I want single(matching) row from an result using id

onEditClick = (event) => {
  console.log(event.target.id);
}
2
  • do I understand correctly that you want to get object by id? Commented Nov 27, 2020 at 5:45
  • Yes. You are right. Commented Nov 27, 2020 at 5:53

1 Answer 1

2

Provided event.target.id contains the relevant ID, you can use array method find():

onEditClick = (event) => {
  const clickedResult = result.find(({id})=>id===parseInt(event.target.id));
  console.log('clickedResult', clickedResult);
}

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

1 Comment

When I convert event.target.id to parseInt(event.target.id) then this will works..

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.