Javascript 将对象数组映射到按时间戳分组的新结构
给定以下对象数组:Javascript 将对象数组映射到按时间戳分组的新结构,javascript,arrays,sorting,Javascript,Arrays,Sorting,给定以下对象数组: 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 }, { tim
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数组=[
{
时间戳:“2016-01-01”,
itemID:'AA',
价值:20,
},
{
时间戳:“2016-01-01”,
itemID:'AB',
数值:32,
},
{
时间戳:“2016-01-02”,
itemID:'ABC',
价值:53
},
{
时间戳:“2016-01-02”,
itemID:'ABCD',
数值:51
}
]
var-res=[];
array.forEach(函数(元素){
var e=res.find(函数(e){
返回e.timestamp==element.timestamp;
});
控制台日志(e)
如果(e){
}否则{
再推({
[element.timestamp]:element.timestamp,
[element.itemID]:element.value
});
}
});
控制台日志(res')代码>您可以按时间戳创建对象分组并将其映射回数组,如:
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 }));
您需要在不存在时插入,并在更改时更新:
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);
}
}
您可以使用映射来收集值
var数组=[{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}],
分组=数组.reduce((map=>(r,a)=>{
var o={timestamp:a.timestamp};
如果(!map.has(a.timestamp)){
map.set(a.时间戳,o);
r、 推(o);
}
map.get(a.timestamp)[a.itemID]=a.value;
返回r;
})(新地图),[]);
控制台日志(分组)代码>
}))
const数组=[
{
时间戳:“2016-01-01”,
itemID:'AA',
价值:20,
},
{
时间戳:“2016-01-01”,
itemID:'AB',
数值:32,
},
{
时间戳:“2016-01-02”,
itemID:'ABC',
价值:53
},
{
时间戳:“2016-01-02”,
itemID:'ABCD',
数值:51
}
]
var-res=[];
array.forEach(函数(元素){
var e=res.find(函数(e){
返回e.timestamp==element.timestamp;
});
如果(e==未定义){
再推({
“timestamp”:element.timestamp,
[element.itemID]:element.value
});
}否则{
e[element.itemID]=element.value;
}
});
控制台日志(res')代码>您可以执行以下操作:
var数组=[
{
时间戳:“2016-01-01”,
itemID:'AA',
价值:20,
},
{
时间戳:“2016-01-01”,
itemID:'AB',
数值:32,
},
{
时间戳:“2016-01-02”,
itemID:'ABC',
价值:53
},
{
时间戳:“2016-01-02”,
itemID:'ABCD',
数值:51
}
],
中间=数组.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},{});
结果=Object.keys(临时)
.map(k=>中间[k]);
控制台日志(结果)代码>
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);
}
});
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
});
}