gpt4 book ai didi

javascript - React-Native - Object.assign 子项更改

转载 作者:行者123 更新时间:2023-12-02 14:25:18 28 4
gpt4 key购买 nike

抱歉,如果这个问题仍然存在,但我搜索了它,但没有找到解决方案。另外,我无法从 Developer.Mozilla 中得到任何提示。网站,其中对 Objects.assign 等进行了很好的描述。

The question:

How can i change the children of an object? With exactly the output i described in the following.

示例代码

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 设置为变量,因为我的代码中有多个键,我必须为多个子项设置这些键。 (做一些过滤工作)

感谢您的帮助

BR乔纳森

最佳答案

首先,不要使用Object作为变量名称..

好吧,假设对象现在是 obj

不修改obj

var newObj = Object.assign({}, obj, {test: newChildren})

或者只是,(修改obj)

obj.test = newChildren

这是你要找的吗?

关于javascript - React-Native - Object.assign 子项更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38310426/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com