I have this array of object
[
{
"name": "Alice",
"age": 10
},
{
"name": "Samantha",
"age": 20
},
{
"name": "Mary",
"age": 19
}
]
How can I update Alice's age to 11?
I tried using map of es6
const newage = 11;
const newDate = person.map(obj =>
return 'Alice' === obj.name ? obj.age= newage : obj
)
The reason why I map instead of normal for loop is that I do not want to mutate the origin person object, is that correct?
.mapwill create a new array, butobj.age= newagewill still mutate the object..mapalready existed before ES6 btw.