In the below array of arrays I need to update the id value for a particular name.
objArray = [{ name: John, id: 12}, { name: Jake, id: 45}, { name: Jacob, id: 78}];
Ex: If user enters a name and id in text boxes and clicks submit I want to update the id in the array for the particular name.
I can accomplish this using for loops but can you please let me know the most efficient way to do this in Java Script/React?
Use the find
function and set the entered name.
let objArray = [{ name: "John", id: 12}, { name: "Jake", id: 45}, { name: "Jacob", id: 78}];
let name = 'Jake'
let newId = 59;
objArray.find((o) => o.name === name).id = newId;
console.log(objArray);
.as-console-wrapper {
max-height: 100% !important
}