我如何在困难的数组中计算总计

How i can make Totals in difficult array

我需要不同任务的帮助(对我来说不同,因为我是 JS 新手) 我有数组:

let results = [
    {
        "id": "su-1617788623816",
        "name": "СУ 2 Мельница",
        "dist": "57",
        "results":
        [
            {"crew":"13","disc":"60414658612aea0018a1f08e","time":40271011,"timeP":0,"speed":"111","shod":false,"reason":""},
            {"crew":"52","disc":"60414658612aea0018a1f08e","time":4815050,"timeP":0,"speed":"170","shod":false,"reason":""}
        ]
    },
    {
        "id": "su-1617788623816",
        "name": "СУ 2 Мельница",
        "dist": "57",
        "results":
        [
            {"crew":"13","disc":"60414658612aea0018a1f08e","time":10271011,"timeP":0,"speed":"111","shod":false,"reason":""},
            {"crew":"52","disc":"60414658612aea0018a1f08e","time":1815050,"timeP":0,"speed":"170","shod":false,"reason":""}
        ]
    }
]

我必须得到这样的结果:

let result = [
    {
        "id": "su-1617788623816",
        "name": "СУ 2 Мельница",
        "dist": "57",
        "results":
        [
            {"crew":"13","disc":"60414658612aea0018a1f08e","time":X1,"timeP":0,"speed":"111","shod":false,"reason":""},
            {"crew":"52","disc":"60414658612aea0018a1f08e","time":X2,"timeP":0,"speed":"170","shod":false,"reason":""}
        ],
        "total":
        [
            {"crew":"13","disc":"60414658612aea0018a1f08e","time":X1,"timeP":0,"speed":"111","shod":false,"reason":""},
            {"crew":"52","disc":"60414658612aea0018a1f08e","time":X2,"timeP":0,"speed":"170","shod":false,"reason":""}
        ]
    },
    {
        "id": "su-1617788623816",
        "name": "СУ 2 Мельница",
        "dist": "57",
        "results":
        [
            {"crew":"13","disc":"60414658612aea0018a1f08e","time":Y1,"timeP":0,"speed":"111","shod":false,"reason":""},
            {"crew":"52","disc":"60414658612aea0018a1f08e","time":Y2,"timeP":0,"speed":"170","shod":false,"reason":""}
        ],
        "total":
        [
            {"crew":"13","disc":"60414658612aea0018a1f08e","time":Y1+X1,"timeP":0,"speed":"111","shod":false,"reason":""},
            {"crew":"52","disc":"60414658612aea0018a1f08e","time":Y2+X2,"timeP":0,"speed":"170","shod":false,"reason":""}
        ]
    }
]

我没有任何想法,结果可能会超过 2。在每一步上,我必须有当前总步数 + 之前的步数

你可以这样做:

      results.forEach((ele,index)=>{
let total = results[index].results;
for(let i = 0;i<=index;i++){
results[index].results.forEach((element,index)=>{
total[index].time += element.time;
})

}
results[index].total=total;
    })

我想这应该行得通!

更新:

上面代码中存在参考问题,解决方法如下:

var resultsCopy = JSON.parse(JSON.stringify(results));
resultsCopy.forEach((ele,index)=>{
let total = resultsCopy[index].results;
for(let i = 0;i<=index;i++){
resultsCopy[index].results.forEach((element,index)=>{
total[index].time += element.time;
})

}
results[index].total=total;
    })

使用数组保存累计次数。然后,您可以通过复制相应的 results 数组并将 time 属性 替换为累计次数来创建 time 数组。

let results = [
    {
        "id": "su-1617788623816",
        "name": "СУ 2 Мельница",
        "dist": "57",
        "results":
        [
            {"crew":"13","disc":"60414658612aea0018a1f08e","time":40271011,"timeP":0,"speed":"111","shod":false,"reason":""},
            {"crew":"52","disc":"60414658612aea0018a1f08e","time":4815050,"timeP":0,"speed":"170","shod":false,"reason":""}
        ]
    },
    {
        "id": "su-1617788623816",
        "name": "СУ 2 Мельница",
        "dist": "57",
        "results":
        [
            {"crew":"13","disc":"60414658612aea0018a1f08e","time":10271011,"timeP":0,"speed":"111","shod":false,"reason":""},
            {"crew":"52","disc":"60414658612aea0018a1f08e","time":1815050,"timeP":0,"speed":"170","shod":false,"reason":""}
        ]
    }
]


let total_time = Array(results[0].results.length).fill(0);

let result = results.map(res => {
  let time = res.results.map((r, i) => {
    total_time[i] += r.time;
    return { ...r, time: total_time[i]
    }
  });
  return { ...res, time: time
  };
});

console.log(result);

首先定义函数:

function reducer(acc, item) {
  let totals = !acc.length ? [...item.results] : [
    {...item.results[0], time: item.results[0].time + acc[acc.length - 1].results[0].time},
    {...item.results[1], time: item.results[1].time + acc[acc.length - 1].results[1].time},
  ];
  acc.push({...item, totals});
  return acc;
}

然后像这样使用它:

let resultWithTotals = results.reduce(reducer, []);
result[0]["total"] = result[0].results; 

for (let i = 1; i < result.length; i++) {
    let newTotal = JSON.parse(JSON.stringify(result[i - 1]["total"]));
    for (let j = 0; newTotal && j < newTotal.length; j++) {
        newTotal[j].time += result[i].results[j].time; // assuming your X1, Y1, X2, Y2 are numbers
    }
    result[i]["total"] = newTotal;
}

console.log(result);