I can go with find for editing one property like in this example. But I didn't find a syntax to edit more than one property.
const arr = [
{
id: 101,
name: “Jone”,
age: 1
},
{
id: 102,
name: “Jane”,
age: 2
},
{
id: 103,
name: “johnny”,
age: 3
}
];
console.log(arr);
arr.find(element => element.id == 101).name = “jimmy”
console.log(arr);
CodePudding user response:
For example, using Object.assign
const arr = [
{
id: 101,
name: 'Jone',
age: 1
},
{
id: 102,
name: 'Jane',
age: 2
},
{
id: 103,
name: 'johnny',
age: 3
}
];
Object.assign(arr.find(el => el.id === 101), {
name: "Jimmy",
age: 5,
});
console.log(arr);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You can store the object that find returns and edit it as you need.
const arr = [
{
id: 101,
name: 'Jone',
age: 1
},
{
id: 102,
name: 'Jane',
age: 2
},
{
id: 103,
name: 'johnny',
age: 3
}
];
console.log(arr);
// ****
const found = arr.find(element => element.id == 101)
found.name = 'jimmy'
found.age = 54
// ****
console.log(arr);