javascript - 完全合并两个多维数组并生成一个新数组

标签 javascript arrays node.js

我有两个数组想要合并,但还要添加数组的“计数”和“总计”部分。

数组一是:(较小)

[ { ReasonCode: '', Count: 2, Total: 15.63 },
  { ReasonCode: '01', Count: 13, Total: -144 },
  { ReasonCode: '03', Count: 7, Total: -394.87 },
  { ReasonCode: '04', Count: 128, Total: -3556.1 },
  { ReasonCode: '07', Count: 2, Total: -4.83 },
  { ReasonCode: '09', Count: 192, Total: -20826.25 } ]

数组二是:(更大)

[ { ReasonCode: '', Count: 6, Total: 412.21 },
  { ReasonCode: '01', Count: 7, Total: -9.75 },
  { ReasonCode: '02', Count: 5, Total: -37.03 },
  { ReasonCode: '04', Count: 162, Total: -1199.16 },
  { ReasonCode: '05', Count: 1, Total: -3.8 },
  { ReasonCode: '06', Count: 2, Total: -58.83 },
  { ReasonCode: '07', Count: 76, Total: -507.23 },
  { ReasonCode: '09', Count: 41, Total: -743.07 } ]

我正在使用此函数来合并它们并创建新数组。

function CombineArrays(BiggerArray, SmallerArray, NewArray) {
  BiggerArray.forEach(function (BA) {
    let match = false;

    SmallerArray.forEach(function (SA) {

        if(BA.ReasonCode === SA.ReasonCode){

            match = true;

            BA.Count += SA.Count;
            BA.Total += SA.Total;
            BA.ReasonCode = SA.ReasonCode;

            NewArray.push(BA);

        }
    });

    if(!match) NewArray.push(BA);

  });
}

由该函数创建的数组缺少第一个数组中的“ReasonCode: '03'”。我怎样才能做到这一点,以便它将添加两个数组中的所有数组对象并将所需的两列加在一起。

[ { ReasonCode: '', Count: 8, Total: 427.84 },
  { ReasonCode: '01', Count: 20, Total: -153.75 },
  { ReasonCode: '02', Count: 5, Total: -37.03 },
  { ReasonCode: '04', Count: 290, Total: -4755.26 },
  { ReasonCode: '05', Count: 1, Total: -3.8 },
  { ReasonCode: '06', Count: 2, Total: -58.83 },
  { ReasonCode: '07', Count: 78, Total: -512.0600000000001 },
  { ReasonCode: '09', Count: 233, Total: -21569.32 } ]

提前谢谢您。

最佳答案

我将循环遍历最小的数组,如果该项目不在大数组中,则添加它,否则添加到Count/Total

const arr1 = [{ ReasonCode: '', Count: 2, Total: 15.63 },
{ ReasonCode: '01', Count: 13, Total: -144 },
{ ReasonCode: '03', Count: 7, Total: -394.87 },
{ ReasonCode: '04', Count: 128, Total: -3556.1 },
{ ReasonCode: '07', Count: 2, Total: -4.83 },
{ ReasonCode: '09', Count: 192, Total: -20826.25 }]

const arr2 = [{ ReasonCode: '', Count: 6, Total: 412.21 },
{ ReasonCode: '01', Count: 7, Total: -9.75 },
{ ReasonCode: '02', Count: 5, Total: -37.03 },
{ ReasonCode: '04', Count: 162, Total: -1199.16 },
{ ReasonCode: '05', Count: 1, Total: -3.8 },
{ ReasonCode: '06', Count: 2, Total: -58.83 },
{ ReasonCode: '07', Count: 76, Total: -507.23 },
{ ReasonCode: '09', Count: 41, Total: -743.07 }]


function CombineArrays(biggest, smallest) {
  // Create a new instance of the bigger array
  let result = [].concat(biggest)
  smallest.forEach(smallItem => {
    // Try to get the item from the bigger list
    var bigItem = result.find(item => item.ReasonCode == smallItem.ReasonCode)
    // If it is not in the bigger list, add it
    if (!bigItem) result.push(smallItem)
    // If it is in the bigger list, then increment count/total by the small item amounts
    else {
      bigItem.Count += smallItem.Count
      bigItem.Total += smallItem.Total
    }
  })
  return result
}

console.log(CombineArrays(arr2, arr1))

关于javascript - 完全合并两个多维数组并生成一个新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50649729/

相关文章:

javascript - 嵌套 "const"JavaScript 值是否有命名约定?

arrays - 检查用户MATLAB是否重复输入坐标

javascript - 按属性值对 JavaScript 对象数组进行排序

node.js - 获取 node.js 中当前定义的函数列表

javascript - 击败这个 Javascript 预防措施有多容易

javascript - 完全迷失在 AngularJS 身份验证实现上

java - carrays.i 中的 SWIG 数组类是否释放它们包装的 C 分配数组?

mysql - Node mysql批量插入带有express数组参数

javascript - 使用 RequireJS 分离脚本

javascript - 随机错误 "Unexpected token in JSON"