javascript - 如何合并两个json数组的键值对? - JavaScript

标签 javascript javascript-objects

我有两个这样的 JSON 数组。我想合并他们的键值对。它们是一些在两者中都很常见的项目,同时也是一些不常见的项目。

var jsonData1 = [
  {
    firstName: "Sam",
    age: "10"
  },
  {
    firstName: "John",
    age: "11"
  },
  {
    firstName: "Jack",
    age: "12"
  },
  {
    firstName: "Pam",
    age: "13"
  },
  {
    firstName: "Tom",
    age: "14"
  },
  {
    firstName: "Mitch",
    age: "15"
  }
];

var jsonData2 = [
      {
        firstName: "Sam",
        city: "London"
      },
      {
        firstName: "John",
        city: "New York"
      },
      {
        firstName: "Jack",
        city: "Paris"
      },
      {
        firstName: "Pam",
        city: "Moscow"
      },
      {
        firstName: "Roger",
        city: "Shanghai"
      },
      {
        firstName: "Don",
        city: "Jakarta"
      }
    ];

正如您所见,第一个数组中有一些名字,第二个数组中没有城市。同样,第二个数组中有一些名字,第一个数组中没有城市。

我需要将这两个数组合并到一个数组中,如果firstName没有年龄或城市,它将被分配为“”(空白字符串)。

新数组将有 3 个字段,有些项目将在 2 个字段中具有值。它们有一个字段作为空白字符串。

我想使用 Vanilla JS 来完成此操作,我不想使用 Jquery、Lodash 和 Underscore。

最佳答案

有多种方法可以解决此问题,但一种选择是围绕 Array#reduce() 方法进行此操作,如下所示:

const jsonData1=[{firstName:"Sam",age:"10"},{firstName:"John",age:"11"},{firstName:"Jack",age:"12"},{firstName:"Pam",age:"13"},{firstName:"Tom",age:"14"},{firstName:"Mitch",age:"15"}];
const jsonData2=[{firstName:"Sam",city:"London"},{firstName:"John",city:"New York"},{firstName:"Jack",city:"Paris"},{firstName:"Pam",city:"Moscow"},{firstName:"Roger",city:"Shanghai"},{firstName:"Don",city:"Jakarta"}];


/* 
Use Array#concat() to combine both input arrays before performing
the merge. This will ensure that all items in both arrays are
processed and accounted for during the merge (which is important in
situations where the input arrays differ in length).

Object.values() is used to transform the dictionary resulting from
reduce() to an array
*/
var result = Object.values(
[].concat(jsonData1, jsonData2
).reduce((dict, item) => {

  /* 
  We use Array#reduce is an intermediate step to construct a dictionary
  which maps each unique "item.firstName" to a corresponding object 
  that contains the merged (or yet to be merged) data for this first 
  name
  */
   
  var value = dict[item.firstName] || {}

  /*
  Use Object.assign() to merge existing data for "item.firstName" with
  current item being processed. Also pass default values as first
  argument to ensure all three key values are present in merge result
  */
  value = Object.assign({ firstName : '', age : '', city : ''} , value, item)

  /*
  Update the dictionary with merged data
  */
  dict[item.firstName] = value

  return dict

}, {}))

console.log(result);

关于javascript - 如何合并两个json数组的键值对? - JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54228609/

相关文章:

javascript - 当谈到 JavaScript 和 CSS 时,你如何命名你的类?

javascript - Service Worker 可以在不打开选项卡的情况下更新缓存吗?

javascript - 做这样的菜单的最佳方式

javascript - Lodash 是否与 _.keys 相反?

javascript按对象键过滤并返回嵌套对象键的值

javascript - 获取对象数组中键的名称 - JS

javascript - 合并两个对象并增加 ECMAScript 6 中的键

windows - 适用于 JavaScript 版本 2.1.30324.52 的适用于 Windows 库的 Visual Studio 扩展似乎停留在 "Configuring your system. This might take a while."

javascript - Xpath获取所有带有内部文本的链接

javascript - 如何将我的数据转换为 React Native 的 SectionList 的正确格式?