简体   繁体   English

当从扩展符号访问对象时,如何访问 Javascript 对象的按名称嵌套的属性...?

[英]How to access the nested property-by-name of the Javascript object when the object is accessed from the spread notation ...?

This https://stackoverflow.com/a/4255480/1375882is good answer about the use of bracked notation to access the properties of the Javascript object/array by the name and it also show examples how to access nested properties or elements from the nested arrays.这个https://stackoverflow.com/a/4255480/1375882 是关于使用带括号的表示法通过名称访问 Javascript 对象/数组的属性的好答案,它还显示了如何从嵌套访问嵌套属性或元素的示例数组。

I am trying to use the same approach to access the properties/elements in the construction that uses spread ... operator, eg:我正在尝试使用相同的方法来访问使用 spread ... 运算符的构造中的属性/元素,例如:

var foo = { a: 1, b: 2, c: {x: 999, y:998, z: 997}};
var foo1 = {...foo,
               ['a']: 2,
               ['c']['y']: 1000,
            };

alert(foo1['c']['y']);

But this code does not compile - while the one-level access ['a'] works as expected, the nested access ['c']['y'] does not work with with spread (but it works outside spread) - the code simply does not compile, there are error messages about un-matched { and other obscure messages (eg in jsfiddle).但是此代码无法编译 - 虽然一级访问['a']按预期工作,但嵌套访问['c']['y']不适用于传播(但它可以在传播之外工作)-代码根本无法编译,有关于不匹配{和其他晦涩消息(例如在 jsfiddle 中)的错误消息。

My question is - how to access nested properties/elements within spread?我的问题是 -如何访问传播中的嵌套属性/元素? Maybe other constructions that brackets are necessary?也许其他需要括号的结构?

Context: I am trying to write general code that listend to the cell value changes in the AgGrid and accordingly updates the Redux store: grid displays the array or records and in my update code I should find the right record and the right cell within it - all this happens in Redux reduce with usually uses spread... for correct update of state, eg idiomatic code is:上下文:我正在尝试编写通用代码来监听 AgGrid 中单元格值的变化并相应地更新 Redux 存储:网格显示数组或记录,在我的更新代码中,我应该找到正确的记录和其中的正确单元格 -所有这一切都发生在 Redux reduce 中,通常使用 spread... 来正确更新状态,例如惯用代码是:

case GET_INVOICES_SUCCESS: {
              return {...state,
                 invoiceDate: action.response.data.invoiceDate,
              }
            }

But I should be able to access the individual properties and nested propertes of the state here.但是我应该能够在这里访问状态的各个属性和嵌套属性。 This agGrid in react + redux app is modifying underlying data is the code which I am trying to generalize. react + redux 应用程序中的这个agGrid 正在修改底层数据是我试图概括的代码。

So - you can see that the use of spread... is not luxury for me, it is the best practice for use in Redux reducers and that is why I have this question.所以 - 你可以看到使用 spread... 对我来说并不奢侈,它是在 Redux 减速器中使用的最佳实践,这就是我有这个问题的原因。

You could separate the wanted nested property with an own spreading.您可以将想要的嵌套属性与自己的扩展分开。

 const foo = { a: 1, b: 2, c: { x: 999, y: 998, z: 997 } }, foo1 = { ...foo, a: 2, c: { ...foo.c, y: 1000 }, }; console.log(foo1['c']['y']); console.log(foo1);

You should use the spread operator for the nested properties as well:您还应该对嵌套属性使用扩展运算符:

var foo = { a: 1, b: 2, c: {x: 999, y:998, z: 997}};
var foo1 = {...foo,
               a: 2,
               c: { ...foo.c, y: 1000 },
            };

alert(foo1.c.y);

Also, you don't need the square brackets if the property name is not a variable.此外,如果属性名称不是变量,则不需要方括号。

Thanks Tasos and Nina for suggestions, but I specifically asked for the case when property is accessed by name and that is why the correct answer is this:感谢 Tasos 和 Nina 的建议,但我特别询问了按名称访问属性的情况,这就是为什么正确答案是这样的:

var foo = { a: 1, b: 2, c: {x: 999, y:998, z: {aa: 5, bb: 6}}};
var foo1 = {...foo,
               ['a']: 2,
               ['c']: {...foo['c'], 
                    ['y']: 1000, 
               },
               ['c']: {...foo['c'],
                    ['z']: {...foo['c']['z'],
                          ['bb']: 777,
                           }
               }
            };

alert(foo1['c']['z']['bb']);

I don't know whether I should accept my own answer because without input from the previous answer there would not be my answer.我不知道我是否应该接受我自己的答案,因为如果没有上一个答案的输入,就不会有我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript 中的括号符号如何访问 object 的属性? - How bracket notation in javascript access the property of an object? 如何在 Javascript 中传播嵌套的 object? - How to spread a nested object in Javascript? javascript 对象属性名称为十进制表示法 - javascript object property name as decimal notation Javascript从动态名称访问嵌套对象 - Javascript access nested object from dynamic name 从对象返回访问的属性名称(键) - Return accessed property name (key) from object 如何访问javaScript对象属性NAME(不值VALUE) - How to access javaScript object property NAME not VALUE 在 object 构造期间从嵌套的 object 获取属性名称时,getter 如何在嵌套对象中工作? - How do getters work in nested objects when getting a property name from a nested object during object construction? 如何使用 JavaScript spread... 语法更改对象的一个​​字段,该字段属于数组并通过名称-值对访问? - How to use JavaScript spread... syntax to change the one field of the object, that belongs to the array and is accessed by name-value pair? 如何使具有扩展(...)表示法的对象具有反应性? - How to make an Object with spread (…) notation reactive? 如何使用扩展运算符更改 object 的嵌套属性? - How to change nested property of an object using spread operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM