将对象添加到现有对象中,并在上述对象中添加键/值对
我有一个对象,其中已经有3个对象。我想动态地将对象添加到原始对象中的一个对象之一,然后将键/值对添加到这些动态添加的对象。
const result = {
AV: {},
Furnaces: {},
"Production Lines": {}
};
因此,这是现有的对象,内部的对象。我可以轻松地将键/值添加到“ av”中,因为它简单地
result[AV]["New key"] = value;
运行,并以该炉子的名称添加键/值对,例如:
for (let i = 0; i < furnaces.length; i++) {
let furnaceName = furnace[i];
result["Furnaces"][furnaceName]["Raw Material"] = 5;
};
,但是如果我尝试通过炉子名称的循环
Cannot set property "Raw Material" of undefined to "270000"
I have an object which already has 3 objects inside it. I want to dynamically add objects to one of those objects inside the original object, and then add key/value pairs to these dynamically added objects.
const result = {
AV: {},
Furnaces: {},
"Production Lines": {}
};
So, this is the existing object with the objects inside. I add key/value pairs dinamically to "AV" easily because its simply
result[AV]["New key"] = value;
But if I try to run through a loop of the furnace names and add key/value pairs to the new object with that furnace's name, like so:
for (let i = 0; i < furnaces.length; i++) {
let furnaceName = furnace[i];
result["Furnaces"][furnaceName]["Raw Material"] = 5;
};
it throws an error
Cannot set property "Raw Material" of undefined to "270000"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以只需使用 >
假设您有各种炉子。尝试这个,
you can simply use Object.assign
Assuming you have array of furnaces. Try this,