如何聚合对象属性上的对象数组以便转动:
[ { key: 'black', value: [ '2', '3', '9' ] },
{ key: 'black', value: [ '1' ] },
{ key: 'gold', value: [ '2', '3' ] },
{ key: 'gold', value: [ '1' ] },
{ key: 'red', value: [ '9' ] },
{ key: 'white', value: [ '2', '3' ] },
{ key: 'white', value: [ '1' ] } ]
...进入:
[ { key: 'black', value: [ '1', '2', '3', '9' ] },
{ key: 'gold', value: [ '1', '2', '3' ] },
{ key: 'red', value: [ '9' ] },
{ key: 'white', value: [ '1', '2', '3' ] } ]
...使用 JavaScript?
我觉得应该有一个相当简单的方法来使用 lodash 或 Array.reduce 来做到这一点,但我一生都无法弄清楚如何做到这一点。
您可以使用临时对象来引用组,并在单个循环中返回一个包含结果的数组。
var array = [{ key: 'black', value: ['2', '3', '9'] }, { key: 'black', value: ['1'] }, { key: 'gold', value: ['2', '3'] }, { key: 'gold', value: ['1'] }, { key: 'red', value: ['9'] }, { key: 'white', value: ['2', '3'] }, { key: 'white', value: ['1'] }],
result = [];
array.forEach(function (a) {
if (!this[a.key]) {
this[a.key] = { key: a.key, value: [] };
result.push(this[a.key]);
}
this[a.key].value = this[a.key].value.concat(a.value);
this[a.key].value.sort();
}, {});
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
您可以很轻松地自己编写此内容,无需任何第 3 方的东西:
var input = ...
var reduced = [];
var reducedMap = {};
input.forEach(function(obj) {
if(!reduced.hasOwnProperty(obj.key) {
reduced[obj.key] = [];
}
for(var i = 0; i < obj.value.length; i++) {
if(reduced[obj.key].indexOf(obj.value[i]) < 0) {
reduced[obj.key].push(obj.value[i]);
}
}
});
reduced = Object.keys(reducedMap).map(function(key){
return {
key: key,
value: reducedMap[key].sort()
};
});
您需要先在地图中进行分组,然后生成您想要的输出
var output = {};
obj.forEach(function(val){
output[val.key] = output[val.key] || [];
output[val.key] = output[val.key].concat(val.value);
});
现在产生压缩输出
var finalOutput = Object.keys(output).map(function(val){
return { key: val, value: output[val] };
});
演示
var obj = [{
key: 'black',
value: ['2', '3', '9']
}, {
key: 'black',
value: ['1']
}, {
key: 'gold',
value: ['2', '3']
}, {
key: 'gold',
value: ['1']
}, {
key: 'red',
value: ['9']
}, {
key: 'white',
value: ['2', '3']
}, {
key: 'white',
value: ['1']
}];
var output = {};
obj.forEach(function(val) {
var key = val.key;
var value = val.value;
output[key] = output[key] || [];
output[key] = output[key].concat(val.value);
});
var finalOutput = Object.keys(output).map(function(val) {
return {
key: val,
value: output[val]
};
});
document.body.innerHTML += JSON.stringify(finalOutput,0,4);
您可以使用
groupBy
、map
和 reduce
的组合来创建所需的结果:
var result =
_(input)
.groupBy('key')
.map(function(d, key) {
return {
key: key,
value: _.reduce(d, function(a, dd) {
return a.concat(dd.value);
}, []).sort(),
};
})
.value();
在这里摆弄: https://jsfiddle.net/dw1zzfqt/1/
ES2015版本
var result =
_(input)
.groupBy('key')
.map((d, key) => ({
key,
value: _.reduce(d, (a, dd) => a.concat(dd.value), []).sort()
}))
.value();
var array = [{ key: 'black', value: ['2', '3', '9'] }, { key: 'black', value: ['1'] }, { key: 'gold', value: ['2', '3'] }, { key: 'gold', value: ['1'] }, { key: 'red', value: ['9'] }, { key: 'white', value: ['2', '3'] }, { key: 'white', value: ['1'] }],
result = [];
array.forEach(function (a) {
if (!this[a.key]) {
this[a.key] = { key: a.key, value: [] };
result.push(this[a.key]);
}
this[a.key].value = this[a.key].value.concat(a.value);
this[a.key].value.sort();
}, {});
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
const dedupe = a => [...new Set(a)].sort()
const arr = [
{ key: 'black', value: ['2', '3', '9', '1'] },
{ key: 'black', value: ['1'] },
{ key: 'gold', value: ['2', '3'] },
{ key: 'gold', value: ['1'] },
{ key: 'red', value: ['9'] },
{ key: 'white', value: ['3', '2'] },
{ key: 'white', value: ['1'] }
]
dedupe(arr.map(item => item.key)) // get unique, sorted array of 'key's
.map(key =>
arr
.filter(item => item.key == key) // process each key in turn
.reduce(
(a, c) => ({
...a,
value: dedupe([...c.value, ...a.value]) // aggregate the 'value's for each 'key'
}),
{ key, value: [] }
)
)