合并Java数组而不使用concat [英] Merging Javascript arrays without concat
问题描述
我有两个数组需要组合。我发现结合数组的大多数建议使用 concat
。但是我不想添加到数组的末尾,我需要将 array1
中的键/值对添加到 array2中的每个对象
。
I have two arrays that I need to combine. Most of the advice on combining arrays I can find use concat
. But I don't want to add to the end of an array, I need to add a key/value pair from array1
to each object in array2
.
我需要合并此数组1:
[
"Basket Abandonment",
"Downloads",
"App Version"
]
使用此数组2:
[
{
bottom: {
comp : "",
details, : "3.1.39 22nd Jul 2015",
status : "",
title : "Previous Version",
value : "8.7%"
},
top: {
details: "3.1.40 25th August 2015",
status: "",
comp: "",
title: "Latest Version",
value: "86%",
}
},
{
bottom: {
value: "469",
title: "Total Reviews",
status: "neutral",
comp: "same",
details: "2 New This Week"
},
top: {
details: "Version 3.1.40",
status: "neutral",
comp: "same",
title: "Average Rating",
value: "4.0"
}
},
{
bottom: {
value: "469",
title: "Total Reviews",
status: "neutral",
comp: "same",
details: "2 New This Week"
},
top: {
details: "Version 3.1.40",
status: "neutral",
comp: "same",
title: "Average Rating",
value: "4.0"
}
}
]
在一个新的组合数组中,我需要添加一个 title 使用第一个数组中的值到每个对象
,以便得到的数组看起来像这样:
In a new combined array, I need to add a key of title
to each object
with the value from the first array so that the resulting array looks like this:
[
{
title: "Basket Abandonment",
bottom: {
comp : "",
details, : "3.1.39 22nd Jul 2015",
status : "",
title : "Previous Version",
value : "8.7%"
},
top: {
details: "3.1.40 25th August 2015",
status: "",
comp: "",
title: "Latest Version",
value: "86%",
}
},
{
title: "Downloads",
bottom: {
value: "469",
title: "Total Reviews",
status: "neutral",
comp: "same",
details: "2 New This Week"
},
top: {
details: "Version 3.1.40",
status: "neutral",
comp: "same",
title: "Average Rating",
value: "4.0"
}
},
{
title: "App Version",
bottom: {
value: "469",
title: "Total Reviews",
status: "neutral",
comp: "same",
details: "2 New This Week"
},
top: {
details: "Version 3.1.40",
status: "neutral",
comp: "same",
title: "Average Rating",
value: "4.0"
}
}
]
推荐答案
您可以在第二个数组中执行简单的 for循环
,插入新的 title
属性取自第一个属性。但是,如果您想要一个可以在不修改源代码的情况下为您提供新数组的函数,那么一种解决方案是创建一个新数组映射第二个数组中对象的克隆,第一个中的字符串,类似于:
You can do a simple for-loop
in the second array inserting the new title
property taken from the first one. But, if you want a function that gives you a new array without modifying the sources, then one solution is to make a new array mapping a clone of the objects in the second array with the strings in the first one, something like this:
const mixed = objects.map((obj, index) => (clone = {...obj}, clone.title = titles[index], clone));
这里有一个使用数组函数的示例:
Here you have an example using a function with your arrays:
const titles = [
"Basket Abandonment",
"Downloads",
"App Version"
];
const objects = [
{
bottom: {
comp: "",
details : "3.1.39 22nd Jul 2015",
status: "",
title: "Previous Version",
value: "8.7%"
},
top: {
details: "3.1.40 25th August 2015",
status: "",
comp: "",
title: "Latest Version",
value: "86%",
}
},
{
bottom: {
value: "469",
title: "Total Reviews",
status: "neutral",
comp: "same",
details: "2 New This Week"
},
top: {
details: "Version 3.1.40",
status: "neutral",
comp: "same",
title: "Average Rating",
value: "4.0"
}
},
{
bottom: {
value: "469",
title: "Total Reviews",
status: "neutral",
comp: "same",
details: "2 New This Week"
},
top: {
details: "Version 3.1.40",
status: "neutral",
comp: "same",
title: "Average Rating",
value: "4.0"
}
}
];
const mix = (o, t) => o.map((m, i) => (c = {...m}, c.title = t[i], c));
const mixed = mix(objects, titles);
console.log(mixed);
这篇关于合并Java数组而不使用concat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!