我被卡住了!我有以下代码:
const hours = [
{
"workHour" : "8",
"Item name 1" : 120,
"Item name 2" : 149,
"Item name 3" : 137
},
{
"workHour" : "9",
"Item name 1" : 134,
"Item name 2" : 119,
"Item name 3" : 144
},
{
"workHour" : "10",
"Item name 1" : 60,
"Item name 2" : 86,
"Item name 3" : 83
},
];
const records = [
{ date: new Date(), statistics: hours },
{ date: new Date(), statistics: hours },
{ date: new Date(), statistics: hours },
];
const result = {};
result.chart = [];
records.forEach((record) => {
const items = [];
record.statistics.forEach((hour) => {
for (let key in hour) {
if (key !== 'workHour') {
items.push({
key: hour[key]
})
}
}
});
result.chart.push({
date: record.date,
items
});
});
console.log(result);
我尝试做的是循环记录数组中的每个项目,然后循环每个记录的小时数,将每个项目的所有值相加,例如计算所有小时的总数,我试着得到的结果是:
array = [
{
date: //record date,
"Item name 1" : 314,
"Item name 2" : 354,
"Item name 3" : 364
},
{
date: //record date,
"Item name 1" : 314,
"Item name 2" : 354,
"Item name 3" : 364
},
{
date: //record date,
"Item name 1" : 314,
"Item name 2" : 354,
"Item name 3" : 364
}
];
小时项目键已知的工作示例:
const hours = [
{
"workHour" : "8",
"Item name 1" : 120,
"Item name 2" : 149,
"Item name 3" : 137
},
{
"workHour" : "9",
"Item name 1" : 134,
"Item name 2" : 119,
"Item name 3" : 144
},
{
"workHour" : "10",
"Item name 1" : 60,
"Item name 2" : 86,
"Item name 3" : 83
},
];
const records = [
{ date: new Date(), statistics: hours },
{ date: new Date(), statistics: hours },
{ date: new Date(), statistics: hours },
];
const result = {};
result.records = [];
const calculate = (profits, key) =>
profits
.filter(profit => profit !== null)
.reduce((prevVal, profitVal) => prevVal + profitVal[key], 0) || 0;
records.forEach((record) => {
const items = [];
const { statistics } = record;
result.records.push({
date: record.date,
'Item name 1': calculate(statistics, 'Item name 1'),
'Item name 2': calculate(statistics, 'Item name 2'),
'Item name 3': calculate(statistics, 'Item name 3'),
});
});
console.log(result);
打印:
records = [
{
'Item name 1': 314,
'Item name 2': 354,
'Item name 3': 364,
},
{
'Item name 1': 314,
'Item name 2': 354,
'Item name 3': 364,
},
{
'Item name 1': 314,
'Item name 2': 354,
'Item name 3': 364,
},
]
答案 0 :(得分:2)
我相信这可以解决您的要求,但我觉得还有更多要求。 记录是否都打算使用相同的小时?
我知道它有点密集,所以我添加了一些评论,希望能够说明它是如何工作的。
const hours = [
{ "workHour" : "8", "Item name 1" : 120, "Item name 2" : 149, "Item name 3" : 137 },
{ "workHour" : "9", "Item name 1" : 134, "Item name 2" : 119, "Item name 3" : 144 },
{ "workHour" : "10", "Item name 1" : 60, "Item name 2" : 86, "Item name 3" : 83 }
];
const records = [
{ date: new Date(), statistics: hours },
{ date: new Date(), statistics: hours },
{ date: new Date(), statistics: hours }
];
// ========================
// Using map()
// For each "record" return an object based on "record"
// ========================
var result = records.map(function(record){
// ========================
// Create the initial result that does not depend
// on summing up the "statistics".
//
// We will pass this into the reduce() and it will be the
// initial value of the accumulator
// ========================
var initialResult = {date: record.date};
// ========================
// ========================
// Using reduce()
// itterate over the interesting work items
// summing up the hours
// ========================
var finalResult = record.statistics.reduce(function(acc, item){
Object.keys(item) // an array of keys in our item object
.filter(function(key){ return key !== "workHour"; }) // remove this key
.forEach(function(key) { acc[key] = (acc[key] || 0) + item[key] }); // for each remaining key, add its value to running total
// ========================
// return the running total accumulator for use against the next item
// ========================
return acc;
// ========================
}, initialResult);
// ========================
// ========================
// return an object based on record but with accumulated data from the reduce()
// this object becomes an item in the array ultimately returned by map()
// ========================
return finalResult;
// ========================
});
// ========================
console.log(result)

这是一个没有reduce的版本:
const hours = [
{ "workHour" : "8", "Item name 1" : 120, "Item name 2" : 149, "Item name 3" : 137 },
{ "workHour" : "9", "Item name 1" : 134, "Item name 2" : 119, "Item name 3" : 144 },
{ "workHour" : "10", "Item name 1" : 60, "Item name 2" : 86, "Item name 3" : 83 }
];
const records = [
{ date: new Date(), statistics: hours },
{ date: new Date(), statistics: hours },
{ date: new Date(), statistics: hours }
];
var result = records.map(function(record){
var initialResult = {date: record.date};
record.statistics.forEach(function(item){
Object.keys(item)
.filter(function(key){ return key !== "workHour"; })
.forEach(function(key) { initialResult[key] = (initialResult[key] || 0) + item[key] });
});
return initialResult;
});
console.log(result)

答案 1 :(得分:1)
如果正确解释问题,您可以将for..of
循环替换为.forEach()
,使用计算属性名称[key]
来设置推送到items
数组的对象的属性名称
const hours = [{
"workHour": "8",
"Item name 1": 120,
"Item name 2": 149,
"Item name 3": 137
}, {
"workHour": "9",
"Item name 1": 134,
"Item name 2": 119,
"Item name 3": 144
}, {
"workHour": "10",
"Item name 1": 60,
"Item name 2": 86,
"Item name 3": 83
}, ];
const records = hours.map((hour, index) => ({
date: new Date(),
statistics: hours[index]
}));
const result = {};
result.chart = [];
records.forEach((record, index) => {
const items = [];
for (let {statistics:hour} of [record]) {
for (let key in hour) {
if (key !== 'workHour') {
items.push({
[key]: hour[key]
+ (result.chart[index -1]
&& result.chart[index -1].hasOwnProperty("items")
? result.chart[index-1].items
.filter(obj => obj.hasOwnProperty(key))[0][key]
: 0)
})
}
}
};
result.chart.push({
date: record.date,
items
});
});
console.log(result);