[英]How to merge objects in a subArray such that there is one array left with multiple objects in javascript
I have the following array seen in full at ARRAY 1 , I need each subarray to merge the objects within it such that it is like so.我在 ARRAY 1处完整看到了以下数组,我需要每个子数组来合并其中的对象,使其像这样。
ARRAY FINAL数组最终
[
{"col0":"glasses","col1":"Milky glasses","col2":"292516467012796941"}
, ...
]
So the end result is one array with 6 objects.所以最终结果是一个包含 6 个对象的数组。 The... above represents the rest of the objects.
上面的...代表对象的rest。
I have tried [].concat.apply([], array)
but it doesn't do quite what I want.我已经尝试过
[].concat.apply([], array)
但它并没有完全达到我想要的效果。 I will post what it does below this array at ARRAY 2我将在 ARRAY 2 的此数组下方发布它的作用
ARRAY 1阵列1
[
[
{
"col0": "Glasses"
},
{
"col1": "Milky Glasses"
},
{
"col2": "292516467012796941"
}
],
[
{
"col0": "Knives"
},
{
"col1": "Milky Knives"
},
{
"col2": "292516484536599049"
}
],
[
{
"col0": "Forks"
},
{
"col1": "Milky Forks"
},
{
"col2": "292516497196057096"
}
],
[
{
"col0": "Gloves"
},
{
"col1": "Kewl Gloves"
},
{
"col2": "292534063457108493"
}
],
[
{
"col0": "Wrench"
},
{
"col1": "Kewl Wrench"
},
{
"col2": "292534088244396552"
}
],
[
{
"col0": "Monkey snake"
},
{
"col1": "Kewl Monkey snake"
},
{
"col2": "292534109863936521"
}
]
]
This is the output that I don't want, but all I could manage thus far.这是我不想要的 output,但到目前为止我能做到的。 See the output I do want at the top at ARRAY FINAL
请参阅我想要的 output 在 ARRAY FINAL 的顶部
ARRAY 2阵列 2
[
{
"col0": "Glasses"
},
{
"col1": "Milky Glasses"
},
{
"col2": "292516467012796941"
},
{
"col0": "Knives"
},
{
"col1": "Milky Knives"
},
{
"col2": "292516484536599049"
},
{
"col0": "Forks"
},
{
"col1": "Milky Forks"
},
{
"col2": "292516497196057096"
},
{
"col0": "Gloves"
},
{
"col1": "Kewl Gloves"
},
{
"col2": "292534063457108493"
},
{
"col0": "Wrench"
},
{
"col1": "Kewl Wrench"
},
{
"col2": "292534088244396552"
},
{
"col0": "Monkey snake"
},
{
"col1": "Kewl Monkey snake"
},
{
"col2": "292534109863936521"
}
]
Thanks for any help ahead of time感谢您提前提供任何帮助
You could map the array and spread the objects for getting a single object.您可以 map 数组并传播对象以获得单个 object。
const data = [[{ col0: "Glasses" }, { col1: "Milky Glasses" }, { col2: 292516467012796900 }], [{ col0: "Knives" }, { col1: "Milky Knives" }, { col2: 292516484536599040 }], [{ col0: "Forks" }, { col1: "Milky Forks" }, { col2: 292516497196057100 }], [{ col0: "Gloves" }, { col1: "Kewl Gloves" }, { col2: 292534063457108500 }], [{ col0: "Wrench" }, { col1: "Kewl Wrench" }, { col2: 292534088244396540 }], [{ col0: "Monkey snake" }, { col1: "Kewl Monkey snake" }, { col2: 292534109863936500 }]], result = data.map(a => Object.assign({}, ...a)); console.log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
That could be accomplished like so, with arr
being your original array -这可以像这样完成,
arr
是您的原始数组 -
for (let i = 0; i < arr.length; i++) {
let res = {}, subArr = arr[i];
subArr.forEach((a,ind) => {
Object.assign(res,subArr[0],subArr[1],subArr[2]);
});
arr[i] = res;
}
Nina has the best answer, but it's worth noting that Object.assign() is a shallow copy. Nina 有最好的答案,但值得注意的是 Object.assign() 是浅拷贝。
If the col properties were objects themselves you could use an approach like the one below:如果 col 属性本身就是对象,则可以使用如下方法:
const finish = start.map((x) => ({
...x[0],
...x[1],
...x[2]
}));
If I understand correctly, the...x[0] syntax creates a deep copy of x[0] and we assign that directly to our new object, whereas Object.assign() creates a shallow copy.如果我理解正确,...x[0] 语法会创建 x[0] 的深层副本,我们将其直接分配给新的 object,而 Object.assign() 创建浅层副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.