【问题标题】:How i can make Totals in difficult array我如何在困难的数组中进行总计
【发布时间】:2021-06-04 18:03:43
【问题描述】:

我需要不同任务的帮助(对我来说不同,因为我是 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 属性复制到total 属性,它没有添加任何内容。
  • 在第一把上我投入总时间:x1 在第二步我投入时间:x2+x1 在第三我必须投入时间 = x3+x2+x1(x1 = 第一圈时间,x2= ti,e 在第二圈.....)

标签: javascript node.js arrays mapreduce


【解决方案1】:

使用数组来保存累计次数。然后您可以通过复制相应的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);

【讨论】:

    【解决方案2】:

    首先定义函数:

    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, []);
    

    【讨论】:

      【解决方案3】:
      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);
      

      【讨论】:

        【解决方案4】:

        你可以这样做:

              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;
            })
        

        【讨论】:

        • 谢谢)我尝试过这样的事情,但在这两种情况下我都有:[圆形对象数组]
        • 嘿,看看这个,现在有参考问题,我现在修复它工作正常
        猜你喜欢
        • 2021-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 2010-11-08
        • 2013-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多