[英]Changing the property value after adding it with map to object array does not work
I have an array of objects: 我有一个对象数组:
In the array I wanted to add an extra property to the objects. 在数组中,我想向对象添加一个额外的属性。
I did this with the map function: 我用map函数做到了这一点:
returnArray = returnArray.map((obj) => {
obj.active = "false";
return obj;
});
later on I want to change the value of the new property 'active' to a new value ("true") in a seperated function. 稍后,我想在一个单独的函数中将新属性“ active”的值更改为新值(“ true”)。 But for some reason the value doesn't change. 但是由于某些原因,该值不会改变。
If I change the value of an property that was in the original array, the value does change. 如果我更改了原始数组中某个属性的值,那么该值的确会更改。
var array = getarrayfunction();
array[index].active = "test"; <-- Does not work
array[index].originalProperty = "test"; <-- Does work
Can anyone explain why and how I can fix this? 谁能解释为什么以及如何解决这个问题?
Thanks! 谢谢!
Assign new property to the existing object as follows with quote ( obj['c'] = "false"). 按如下所示用引号(obj ['c'] =“ false”)将新属性分配给现有对象。 http://jsfiddle.net/xpvt214o/488358/ http://jsfiddle.net/xpvt214o/488358/
var returnArray = [{'a':1,'b':'xxx'},{'a':1,'b':'true'}];
$('button').click(function(){
returnArray = returnArray.map((obj) => {
obj['c'] = "false";
return obj;
});
console.log(returnArray);
})
$('p').click(function(){
returnArray[0]['c'] = 'test';
console.log(returnArray);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.