I have a array of object.
The structure of object is as below, having two keys: one is name, the other is a
{name:'set1',a:1}
Below is my programming to handle an add object function into it;
let totalData = []
console.log(totalData)
handleUpdateTotalData = (updateJson) => {
let duplicate = false;
tmpTotalData = TotalData.map((element,index) => {
if( element.name === updateJson.name ){
duplicate = true
return updateJson
}
return element
})
return duplicate? tmpTotalData: (totalData = [...totalData,updateJson])
}
handleUpdateTotalData({name:'set1',a:1})
console.log(totalData)
handleUpdateTotalData({name:'set1', a:2}) //since probety name:'set1' has occured, so it should update the previous object instead of adding a new one
console.log(totalData)
//expected to show {name:'set1', a:2}
//But now it show{name:'set1', a:1}
Q1: In my program, the expected outcome is not as my expected, May I ask why is that?