React-Native - Object.assign children 变化
React-Native - Object.assign children change
对不起,如果这个问题仍然存在,但我搜索了它但没有找到解决方案。我也无法从 Developer.Mozilla 网站得到任何提示,其中 Objects.assign 等描述得很好。
The question:
How can i change the children of an object? With exactly the output i described in the following.
Sample-Code
var obj= {test: [{a: 2}, {b: 3}]};
var newChildren = [{a: 3}, {b: 5}, {c: 1}];
//I read the obj key value by the following:
var objKey = Object.entries(obj)[0][0]
//Here i want to have some code to get the following result
//My momentary regarding to @webdeb
var output = Object.assign({}, {objKey: newChildren})
actual output: // {objKey: [{a: 3}, {b: 5}, {c: 1}]}
wanted output: // {test: [{a: 3}, {b: 5}, {c: 1}]}
我没有其他选择来获取其他格式的代码,因此需要按照我描述的方式进行“输入”。我需要将 objKey
设置为变量,因为我的代码中有多个键,我必须为多个 children 设置。 (做一些过滤的东西)
感谢您的帮助
BR
乔纳森
首先,不要使用Object
作为变量名..
好的,假设对象现在是 obj
不修改obj
var newObj = Object.assign({}, obj, {test: newChildren})
或者只是,(修改 obj
)
obj.test = newChildren
是您要找的吗?
以下解决方案给了我想要的确切结果:
obj[objKey] = newChildren;
console.log(obj) // {test: [{a: 3}, {b: 5}, {c: 1}]}
我对这个问题采取了这个解决方案:
How can I add a key/value pair to a JavaScript object?
对我来说重要的是将 object-key
作为变量。所描述的解决方案使我可以选择向 more
添加一个值,而不仅仅是一个 static
键。
对不起,如果这个问题仍然存在,但我搜索了它但没有找到解决方案。我也无法从 Developer.Mozilla 网站得到任何提示,其中 Objects.assign 等描述得很好。
The question:
How can i change the children of an object? With exactly the output i described in the following.
Sample-Code
var obj= {test: [{a: 2}, {b: 3}]};
var newChildren = [{a: 3}, {b: 5}, {c: 1}];
//I read the obj key value by the following:
var objKey = Object.entries(obj)[0][0]
//Here i want to have some code to get the following result
//My momentary regarding to @webdeb
var output = Object.assign({}, {objKey: newChildren})
actual output: // {objKey: [{a: 3}, {b: 5}, {c: 1}]}
wanted output: // {test: [{a: 3}, {b: 5}, {c: 1}]}
我没有其他选择来获取其他格式的代码,因此需要按照我描述的方式进行“输入”。我需要将 objKey
设置为变量,因为我的代码中有多个键,我必须为多个 children 设置。 (做一些过滤的东西)
感谢您的帮助
BR 乔纳森
首先,不要使用Object
作为变量名..
好的,假设对象现在是 obj
不修改obj
var newObj = Object.assign({}, obj, {test: newChildren})
或者只是,(修改 obj
)
obj.test = newChildren
是您要找的吗?
以下解决方案给了我想要的确切结果:
obj[objKey] = newChildren;
console.log(obj) // {test: [{a: 3}, {b: 5}, {c: 1}]}
我对这个问题采取了这个解决方案:
How can I add a key/value pair to a JavaScript object?
对我来说重要的是将 object-key
作为变量。所描述的解决方案使我可以选择向 more
添加一个值,而不仅仅是一个 static
键。