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

标签 javascript object react-native assign

抱歉,如果这个问题仍然存在,但我搜索了它,但没有找到解决方案。另外,我无法从 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/

相关文章:

javascript - 带有 eventListener 的原型(prototype)内的 Array.from(..) 不起作用

javascript - 关闭所有 css3

在没有对象初始化的情况下调用 C++ 函数

javascript - Angular - 来自变量的 ngModel 对象属性

css - 无论原始大小如何,我都需要一个图像来占据它可以占用的最大空间

android - adb 服务器版本 (39) 与此客户端 (40) 不匹配;杀...亚行服务器没有确认

javascript - jQuery - 删除值复选框选择

javascript - Materialise(框架)字符数

jQuery:this.attr() 不是一个函数?

reactjs - React Native 选择器和控制台显示相同状态的不同值