[英]Modify nested object and then return the modified parent object
I've got this function 我有这个功能
function save(value, ...props)
{
var record;
var allRecords = JSON.parse(window.localStorage.getItem("data"));
record = allRecords;
for (var prop of props)
{
record = record[prop];
}
record = value;
window.localStorage.setItem("data", JSON.stringify(allRecords));
}
whose job is to save a bunch of data in the window.localStorage. 其工作是在window.localStorage中保存一堆数据。 However, since JavaScript is a 'reference by value' language, modifying
record
doesn't affect the parent object ( allRecords
). 但是,由于JavaScript是一种“按值引用”语言,因此修改
record
不会影响父对象( allRecords
)。 So, how can I iterate throughout the storage object, modifying one its children, and then save the modified parent object? 那么,如何遍历整个存储对象,修改其子对象之一,然后保存修改后的父对象?
You need to modify the parent object: 您需要修改父对象:
function save(value, ...props)
{
var record = allRecords = JSON.parse(window.localStorage.getItem("data"));
var parent=record;
for (var prop of props)
{
parent=record;//store before modifying, so keep the parent
record = record[prop];
}
parent[prop]= value;
window.localStorage.setItem("data", JSON.stringify((allRecords));
return parent;
}
However you now that your function goes "deep" into the object ( just saying): 但是,现在您可以将函数“深入”到对象中(只是说):
save("hi","a","b","c");//will save like this
a={
b={
c="hi";
}
}
Another possibility would be stop the loop right before the last iteration, then do record[prop]=value. 另一种可能是在最后一次迭代之前停止循环,然后执行record [prop] = value。
1.How objects are stored: Objects are stored by pointer. 1.对象的存储方式:通过指针存储对象。 So in memory it looks like this (pseudocode):
因此在内存中看起来像这样(伪代码):
//a memory location holding an object
432:
val1:1
val2:2
If you say the name of the object is a, a is stored like this: 如果您说对象的名称为a,则存储a的方式如下:
a = location:432
if you copy it, for example with b=a, just the pointer is copied not the object: 如果复制它(例如使用b = a),则仅复制指针而不复制对象:
a = location:432
b = location:432
If you change sth in the object a, it will in fact change the object at location 432, therefore b is changed to. 如果更改对象a中的sth,实际上它将更改位置432处的对象,因此将b更改为。 Objects in Objects look the same:
对象中的对象外观相同:
//another obj
100:
obj1: location:432 //our pointer to the object
So our for loop (lets stay at the upper save example) will follow pointer to an object, get a property holding a pointer, than follow this pointer to another object ( a-> b -> c). 因此,我们的for循环(让我们留在最上面的保存示例中)将跟随对象的指针,获得持有指针的属性,而不是将此指针跟随另一个对象(a-> b-> c)。 These objects are stored somewhere in the mem.
这些对象存储在内存中的某个位置。 The JSON.stringify function does deep copy.
JSON.stringify函数执行深层复制。 So it follows every pointer and puts every necessary info into one string ( object ab and c).
因此,它跟随每个指针并将每个必要的信息放入一个字符串(对象ab和c)。
What you do 你做什么
value=obj[key];
value="new";
What i do: 我所做的:
obj[key]="new";
But thats the same isnt it? 但是那是一样的吗? NOPE.
不。 Lets look at the data in memory:
让我们看一下内存中的数据:
//you
//before
value:undefined
obj:
key:"old"
//copying
value:"old"
obj:
key:"old"
//changing
value:"new"
obj:
key:"old" //never changed...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.