我有两个需要合并的数组:
阵列1:
[ { 'Issues in Version': 'PPW-1075', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1076', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1077', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1078', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1079', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1080', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1081', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1123', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1133', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1134', 'Issues In Progress': '' } ]
阵列2:
[{ 'Issues In Version': '', 'Issues In Progress': 'PPW-1123' },
{ 'Issues In Version': '', 'Issues In Progress': 'PPW-1133' },
{ 'Issues In Version': '', 'Issues In Progress': 'PPW-1134' } ]
如何合并这些数组以提供以下内容:
[ { 'Issues in Version': 'PPW-1075', 'Issues In Progress': 'PPW-1123' },
{ 'Issues in Version': 'PPW-1076', 'Issues In Progress': 'PPW-1133' },
{ 'Issues in Version': 'PPW-1077', 'Issues In Progress': 'PPW-1134' },
{ 'Issues in Version': 'PPW-1078', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1079', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1080', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1081', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1123', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1133', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1134', 'Issues In Progress': '' } ]
答案 0 :(得分:1)
Yocu可以生成一个新数组,检查属性是否具有匹配属性的真值。
var array1 =[ { 'Issues In Version': 'PPW-1075', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1076', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1077', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1078', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1079', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1080', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1081', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1123', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1133', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1134', 'Issues In Progress': '' } ],
array2 = [{ 'Issues In Version': '', 'Issues In Progress': 'PPW-1123' }, { 'Issues In Version': '', 'Issues In Progress': 'PPW-1133' }, { 'Issues In Version': '', 'Issues In Progress': 'PPW-1134' } ],
result = array1.map((o, i) => ({
'Issues In Version': o['Issues In Version'] || ((array2[i] || {})['Issues In Version']) || '',
'Issues In Progress': o['Issues In Progress'] || ((array2[i] || {})['Issues In Progress']) || ''
}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
任何长度数组的解决方案。
var array1 =[ { 'Issues In Version': 'PPW-1075', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1076', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1077', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1078', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1079', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1080', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1081', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1123', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1133', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1134', 'Issues In Progress': '' } ],
array2 = [{ 'Issues In Version': '', 'Issues In Progress': 'PPW-1123' }, { 'Issues In Version': '', 'Issues In Progress': 'PPW-1133' }, { 'Issues In Version': '', 'Issues In Progress': 'PPW-1134' } ],
result = [array1, array2].reduce((r, a) => (a.forEach((o, i) => {
r[i] = r[i] || {};
Object.keys(o).forEach(k => r[i][k] = r[i][k] || o[k] || '');
}), r), []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
如果您想保留相同的数组,只想添加新属性
var array1 = [{ 'Issues In Version': 'PPW-1075', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1076', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1077', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1078', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1079', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1080', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1081', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1123', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1133', 'Issues In Progress': '' }, { 'Issues In Version': 'PPW-1134', 'Issues In Progress': '' } ]
var array2 = [{ 'Issues in Version': '', 'Issues In Progress': 'PPW-1123' }, { 'Issues In Version': '', 'Issues In Progress': 'PPW-1133' }, { 'Issues In Version': '', 'Issues In Progress': 'PPW-1134' } ]
for (let i = 0, l = array2.length; i < l; i++) {
array1[i]['Issues In Progress'] = array2[i]['Issues In Progress']
}
console.log(array1)
答案 2 :(得分:0)
这个怎么样?
const first = [
{ 'Issues in Version': 'PPW-1075', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1076', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1077', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1078', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1079', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1080', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1081', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1123', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1133', 'Issues In Progress': '' },
{ 'Issues in Version': 'PPW-1134', 'Issues In Progress': '' }
];
const second = [
{ 'Issues In Version': '', 'Issues In Progress': 'PPW-1123' },
{ 'Issues In Version': '', 'Issues In Progress': 'PPW-1133' },
{ 'Issues In Version': '', 'Issues In Progress': 'PPW-1134' }
];
const merged = new Array(Math.max(first.length, second.length))
.fill(null)
.map((item, index) => ({
'Issues in Version': index < first.length ? first[index]['Issues in Version'] : '',
'Issues in Progress': index < second.length ? second[index]['Issues In Progress'] : ''
}));
您创建一个长度 n 的数组,其中 n 是2个数组长度的最大值。如果不能保证第一个数组的长度等于或大于第二个数组,则必须这样做。
使用占位符值(在本例中为null
)填充数组,以便能够迭代它。
您使用每个迭代的map
i 将占位符数组index
添加到所需对象的数组中,以获取Issues in Version
的值 i 第一个数组中的第一个项目以及第二个数组的 i 项中的Issues In Progress
值。三元运算符确保在index
超过数组长度的情况下,使用空字符串。