如何使用 javascript 从对象数组中获取所需数据?
How can i get my required data from array of objects using javascript?
我有这样的对象数组
var a = [
{'time' : 1539664755070,'T-1': 23 },
{'time' : 1539665095442,'H-1': 24 },
{'time' : 1539666489560,'T-1': 42 },
{'time' : 1539665095442,'H-1': 27 },
{'time': 1539671682230,'H-1': 40.45,'T-2': 33},
{'time': 1539671682230,'T-2': 30.45,'T-1': 65},
{'time': 1539671682230,'T-2': 42.45,'H-1': 11},
{'time': 1539671682230,'T-1': 50.45,'T-2': 85}
];
我想要这样的数据
data : {
'T-1' : [23,42,50.45],
'T-2' : [33,30.45,85],
'H-1' : [24,27,40.45,11]
}
如何从给定数据中获取这些数据?
const data = a.reduce((acc, row) => {
// Loop over keys of each row.
Object.keys(row)
// Filter out the "time" keys.
.filter((key) => key !== 'time')
// Collect the values by key, storing them in the accumulator.
.forEach((key) => {
if (typeof acc[key] === 'undefined') {
acc[key] = []
}
acc[key].push(row[key])
})
return acc
}, {})
这是一个解决方案,如果有什么不明白的地方,请告诉我:
const result = {};
a.forEach(object => {
// Loop on each entry in the original array
Object.entries(object).forEach(([key,value]) => {
// Loop through each entry of one entry of the original array
// (ex 'T-1', 'T-2')
if(key !== 'time') {
// It's a key that I'm interested in, so do logic
if(!result[key]) {
// If the key (example 'T-1') haven't been encountered yet,
// initialize it's entry in the result with an empty array
result[key] = [];
}
result[key].push(value);
}
});
});
您可以使用 array#reduce
和 Object.values()
来创建您的键的散列并添加与之对应的值。然后使用 Object.assign()
和 spread syntax
您可以创建最终对象。
let data = [ {'time' : 1539664755070,'T-1': 23 }, {'time' : 1539665095442,'H-1': 24 }, {'time' : 1539666489560,'T-1': 42 }, {'time' : 1539665095442,'H-1': 27 }, {'time': 1539671682230,'H-1': 40.45,'T-2': 33}, {'time': 1539671682230,'T-2': 30.45,'T-1': 65},{'time': 1539671682230,'T-2': 42.45,'H-1': 11}, {'time': 1539671682230,'T-1': 50.45,'T-2': 85} ],
result = Object.assign({}, ...Object.values(data.reduce((r, o) => {
let keys = Object.keys(o).filter(k => k !== 'time');
keys.forEach(key => {
r[key] = r[key] || {[key] : []};
r[key][key].push(o[key]);
});
return r;
},{})));
console.log(result);
使用 Map 而不是 Key,Value 数组的最佳用例。简化了很多事情。
我有这样的对象数组
var a = [
{'time' : 1539664755070,'T-1': 23 },
{'time' : 1539665095442,'H-1': 24 },
{'time' : 1539666489560,'T-1': 42 },
{'time' : 1539665095442,'H-1': 27 },
{'time': 1539671682230,'H-1': 40.45,'T-2': 33},
{'time': 1539671682230,'T-2': 30.45,'T-1': 65},
{'time': 1539671682230,'T-2': 42.45,'H-1': 11},
{'time': 1539671682230,'T-1': 50.45,'T-2': 85}
];
我想要这样的数据
data : {
'T-1' : [23,42,50.45],
'T-2' : [33,30.45,85],
'H-1' : [24,27,40.45,11]
}
如何从给定数据中获取这些数据?
const data = a.reduce((acc, row) => {
// Loop over keys of each row.
Object.keys(row)
// Filter out the "time" keys.
.filter((key) => key !== 'time')
// Collect the values by key, storing them in the accumulator.
.forEach((key) => {
if (typeof acc[key] === 'undefined') {
acc[key] = []
}
acc[key].push(row[key])
})
return acc
}, {})
这是一个解决方案,如果有什么不明白的地方,请告诉我:
const result = {};
a.forEach(object => {
// Loop on each entry in the original array
Object.entries(object).forEach(([key,value]) => {
// Loop through each entry of one entry of the original array
// (ex 'T-1', 'T-2')
if(key !== 'time') {
// It's a key that I'm interested in, so do logic
if(!result[key]) {
// If the key (example 'T-1') haven't been encountered yet,
// initialize it's entry in the result with an empty array
result[key] = [];
}
result[key].push(value);
}
});
});
您可以使用 array#reduce
和 Object.values()
来创建您的键的散列并添加与之对应的值。然后使用 Object.assign()
和 spread syntax
您可以创建最终对象。
let data = [ {'time' : 1539664755070,'T-1': 23 }, {'time' : 1539665095442,'H-1': 24 }, {'time' : 1539666489560,'T-1': 42 }, {'time' : 1539665095442,'H-1': 27 }, {'time': 1539671682230,'H-1': 40.45,'T-2': 33}, {'time': 1539671682230,'T-2': 30.45,'T-1': 65},{'time': 1539671682230,'T-2': 42.45,'H-1': 11}, {'time': 1539671682230,'T-1': 50.45,'T-2': 85} ],
result = Object.assign({}, ...Object.values(data.reduce((r, o) => {
let keys = Object.keys(o).filter(k => k !== 'time');
keys.forEach(key => {
r[key] = r[key] || {[key] : []};
r[key][key].push(o[key]);
});
return r;
},{})));
console.log(result);
使用 Map 而不是 Key,Value 数组的最佳用例。简化了很多事情。