编程语言
首页 > 编程语言> > javascript-根据匹配的ID合并两个数组中的项目

javascript-根据匹配的ID合并两个数组中的项目

作者:互联网

我有一个像这样的数据对象:

{
  "data1": [
    [
      "ID",
      "name",
      "Birthday"
    ],
    [
      "10",
      "thomas",
      "1992-03-17"
    ],
    [
      "11",
      "Emily",
      "2000-03-03"
    ]
  ],
  "data2": [
    [
      "Balance",
      "ID"
    ],
    [
      "$4500",
      "10"
    ],
    [
      "$1500",
      "13"
    ]
  ]
}

它包含两个数组data1和data2.
每个数组的第一行是列的名称,其余行具有数据(像表一样思考).

我想比较两个数组中的ID字段,如果ID匹配,则最终输出将包含一列Balance,余额与该ID对应,如果ID不匹配,则Balance将为$0.

预期产量:

{
  "output": [
    [
      "ID",
      "name",
      "Birthday",
      "Balance"
    ],
    [
      "10",
      "thomas",
      "1992-03-17",
      "$4500" //ID 10 matched so the balance added here
    ],
    [
      "11",
      "Emily",
      "2000-03-03",
      "0" //0 bcoz the ID 11 is not there in data2 array
    ]
  ]

}

我发现完成这项工作具有挑战性.可以将其视为MySQL中的LEFT-JOIN.
我提到了这个solution,但由于我没有响应中的键,因此在我的情况下不起作用.

编辑:我也需要加入其他领域.

解决方法:

您可以使用Array.prototype.map()findfilterslicereduceconcatincludesObject.assign().

此解决方案:

>处理项目的任意排序.该顺序是从标题中读取的.
>仅当data2中存在一个余额字段时,才追加余额字段.
>联接所有其他字段(OP要求,请参见下面的注释).
>将默认值用作输入,如果data1和data2中不存在数据,则使用它们.

function merge({ data1, data2 }, defaults) {
  // get the final headers, add/move 'Balance' to the end
  const headers = [...data1[0].filter(x => x !== 'Balance')]
    .concat(data2[0].includes('Balance') ? ['Balance'] : []);
  
  // map the data from data1 to an array of objects, each key is the header name, also merge the default values.
  const d1 = data1.slice(1)
    .map(x => x.reduce((acc, y, i) => ({ ...defaults, ...acc, [data1[0][i]]: y }), {}));
  // map the data from data2 to an array of objects, each key is the header name
  const d2 = data2.slice(1)
    .map(x => x.reduce((acc, y, i) => ({ ...acc, [data2[0][i]]: y }), {}));
  
  // combine d1 and d2
  const output = d1.map((x, i) => { // iterate over d1
    // merge values from d2 into this value
    const d = Object.assign(x, d2.find(y => y['ID'] === x['ID']));
    // return an array ordered according to the header
    return headers.map(h => d[h]);
  });
  return { output: [headers, ...output] };
}

const test0 = {
  data1: [[ "ID","name","Birthday","other"],["10","thomas","1992-03-17","empty"],["11","Emily","2000-03-03","empty"]],
  data2: [["other", "ID", "Balance", "city"],["hello", "10", "$4500", "New York"],["world", "10","$8","Brazil"]]
};

const test1 = {
  data1: [["ID","name","Birthday"],["10","thomas","1992-03-17"],["11","Emily","2000-03-03"]],
  data2: [["other","ID"],["x","10"],["y","11"]]
};

console.log(merge(test0, { Balance: '$0' }));
console.log(merge(test1, { Balance: '$0' }));

标签:node-js,left-join,arrays,javascript,sorting
来源: https://codeday.me/bug/20191108/2005720.html