给出以下对象数组:
const array = [
{
timestamp: '2016-01-01',
itemID: 'AA',
value: 20,
},
{
timestamp: '2016-01-01',
itemID: 'AB',
value: 32,
},
{
timestamp: '2016-01-02',
itemID: 'ABC',
value: 53
},
{
timestamp: '2016-01-02',
itemID: 'ABCD',
value: 51
}
]
我想返回以下结果:
const result = [
{
timestamp: '2016-01-01',
AA: 20,
AB: 32,
},
{
timestamp: '2016-01-02',
ABC: 53,
ABCD: 51
}
]
我设法做了以下事情:
const array = [
{
timestamp: '2016-01-01',
itemID: 'AA',
value: 20,
},
{
timestamp: '2016-01-01',
itemID: 'AB',
value: 32,
},
{
timestamp: '2016-01-02',
itemID: 'ABC',
value: 53
},
{
timestamp: '2016-01-02',
itemID: 'ABCD',
value: 51
}
]
var res = [];
array.forEach(function(element) {
var e = res.find(function(e) {
return e.timestamp == element.timestamp;
});
console.log(e)
if(e) {
} else {
res.push({
[element.timestamp]: element.timestamp,
[element.itemID]: element.value
});
}
});
console.log(res, '');
答案 0 :(得分:3)
您可以创建一个对象 - 按时间戳分组并将其映射回数组,例如:
let mapByTimestamp = array.reduce((res, item) => {
res[item.timestamp] = res[item.timestamp] || {};
Object.assign(res[item.timestamp], {
[item.itemID]: item.value
});
return res;
}, {});
Object.keys(mapByTimestamp)
.map(timestamp => Object.assign(mapByTimestamp[timestamp], { timestamp }));
答案 1 :(得分:1)
您需要在不存在时插入并在更改时进行更新:
var output = [];
for (var index in array) {
var found = false;
for (var innerIndex in output) {
if (output[innerIndex].timestamp === array[index].timestamp) {
output[innerIndex][array[index].itemID] = array[index].value;
found = true;
}
}
if (found) {
var newItem = {timestamp: array[index].timestamp};
newItem[array[index].itemID] = array[index].value;
output.push(newItem);
}
}
答案 2 :(得分:1)
您可以使用地图来收集值。
var array = [{ timestamp: '2016-01-01', itemID: 'AA', value: 20 }, { timestamp: '2016-01-01', itemID: 'AB', value: 32 }, { timestamp: '2016-01-02', itemID: 'ABC', value: 53 }, { timestamp: '2016-01-02', itemID: 'ABCD', value: 51 }],
grouped = array.reduce((map => (r, a) => {
var o = { timestamp: a.timestamp };
if (!map.has(a.timestamp)) {
map.set(a.timestamp, o);
r.push(o);
}
map.get(a.timestamp)[a.itemID] = a.value;
return r;
})(new Map), []);
console.log(grouped);

答案 3 :(得分:1)
array.forEach(function(element) {
var e = res.find(function(data) {
return data.timestamp == element.timestamp;
});
if(e) {
e[element.itemID] = element.value;
} else {
res.push({
timestamp: element.timestamp,
[element.itemID]: element.value
});
}
});
答案 4 :(得分:0)
var res = [];
array.forEach(function(element) {
var e = res.find(function(e){
return e.timestamp == element.timestamp;
});
if(e){
e[element.itemID] = element.value;
}
else{
e = {};
e.timestamp = element.timestamp;
e[element.itemID] = element.value;
res.push(e);
}
});
答案 5 :(得分:0)
const array = [
{
timestamp: '2016-01-01',
itemID: 'AA',
value: 20,
},
{
timestamp: '2016-01-01',
itemID: 'AB',
value: 32,
},
{
timestamp: '2016-01-02',
itemID: 'ABC',
value: 53
},
{
timestamp: '2016-01-02',
itemID: 'ABCD',
value: 51
}
]
var res = [];
array.forEach(function(element) {
var e = res.find(function(e) {
return e.timestamp == element.timestamp;
});
if(e === undefined){
res.push({
'timestamp':element.timestamp,
[element.itemID]:element.value
});
}else{
e[element.itemID] = element.value;
}
});
console.log(res, '');
那里,我修复了你的代码。 :)
答案 6 :(得分:0)
您可以执行以下操作;
var array = [
{
timestamp: '2016-01-01',
itemID: 'AA',
value: 20,
},
{
timestamp: '2016-01-01',
itemID: 'AB',
value: 32,
},
{
timestamp: '2016-01-02',
itemID: 'ABC',
value: 53
},
{
timestamp: '2016-01-02',
itemID: 'ABCD',
value: 51
}
],
interim = array.reduce((p,c) => Object.assign(p, {[c.timestamp]: p[c.timestamp] ? Object.assign(p[c.timestamp], {[c.itemID]: c.value})
: {[c.itemID]: c.value, timestamp: c.timestamp}}),{});
result = Object.keys(interim)
.map(k => interim[k]);
console.log(result);