简体   繁体   English

修改嵌套对象,然后返回修改后的父对象

[英]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)。

  1. "Why does your code work and mys not?": “为什么您的代码有效而我的代码无效?”:

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.

相关问题 嵌套object需要修改 - Nested object needs to be modified AngularJS过滤嵌套数组并返回父对象 - AngularJS filter nested array and return parent object 如何通过过滤嵌套属性返回父对象 - How to return parent object by filtering nested property 修改 JSON Object 中的特定值,返回带有修改值的 object 的克隆版本? - Modify specific values within a JSON Object, return a cloned version of the object with modified values? 当子 object 变量被修改并且它的父级和嵌套 JSON 中的大多数父级相同时,如何更新父级 object 变量 - how to update parent object variable when child object variable is modified and same for it's parent and till most parent in nested JSON 返回带有下划线/角的修改对象 - Return modified object with Underscore/Angular 如何按属性在嵌套的 object 中搜索 object 并返回 object 与父对象的键和值? - How to search object in nested object by property and return object with parent object's key and value? JS:从嵌套数组中删除对象并返回父数组 - JS: Remove object from nested array and return parent array 如何在es6中过滤嵌套对象并返回父对象 - how to filter nested object in es6 and return the parent 如何过滤嵌套的 javascript object 并返回父级? - How can I filter a nested javascript object and return the parent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM