[英]Javascript Push array to array with unique key
I have object with array like that:我有这样的数组对象:
const result = {
key1: [],
key2: [],
key3: [],
key4: []
};
And I want to push to one of the "key" something like that:我想推到这样的“关键”之一:
result.key1.push({key11: []})
result.key1.push({key12: []})
result.key1.push({key13: []})
But I need the result looks like that:但我需要的结果是这样的:
{
key1: [
key11: [],
key12: [],
key13: []
],
key2: [],
key3: [],
key4: []
}
I tried almost everything did I miss something?我几乎尝试了所有东西,我错过了什么吗?
You're mixing up objects and arrays.您正在混淆对象和数组。 Arrays have items in order, from 0 to length - 1, while objects have named keys.
数组按顺序排列项目,从 0 到长度 - 1,而对象具有命名键。 It seems that you're looking for having named keys, so you need to create an object instead.
似乎您正在寻找具有命名键的,因此您需要创建一个对象。
const result = {
key1: {},
key2: {},
key3: {},
key4: {},
};
Now simply assign items.现在只需分配项目。
result.key1.key11 = []
result.key1.key12 = []
result.key1.key13 = []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.