I have any array of objects like this
let myObj=[{a:'CR',showMe: true},{a:'PY'}];
Now I'm trying to find the object which has a as CR and showMe as true and want to change the a value.
let findObj = myObj.filter(i=> i.a == 'CR' && i.showMe);
findObj.map(ele => ele['a'] = "PS");
When I'm trying to console myObj,value of a in myObj is changed along with findObj.
I don't want myObj to be changed.
What is causing the issue, could someone help?
myObjbecause currently,findObjis an array of the same elements. A quick and dirty fix islet findObj = JSON.parse(JSON.stringify(myObj)).filter(i=> i.a == 'CR' && i.showMe);(also, you should use.forEachinstead of.mapwhen just want to iterate over the objects){a:'PY'}to be in the result array?