基于键值比较合并两个对象数组

qv7cva1a  于 2021-09-23  发布在  Java
关注(0)|答案(3)|浏览(349)

我试图合并两个对象,它们都有相同相似的键,但值不同。我希望它们保留不同的键,但将它们放在匹配的键值中
这是我的第一个obj,

const obj1 = [
      {
        "p_id": 1,
        "name": "Peter",
        "status" : "Active"
      },
      {
        "p_id": 2,
        "name": "Kane",
        "status" : "Active"
      },
      {
        "p_id": 3,
        "name": "William",
        "status" : "Inactive"
      }
]

}

我的第二个目标,

const obj2 = [
  { p_id: 1, type: 'home', no: '+01 234 5678' },
  { p_id: 1, type: 'work', no: '+09 111 2223' },
  { p_id: 2, type: 'home', no: '+12 345 6789' },
]

实际上我做过这样的事情,

obj1.forEach((item) => {
            Object.assign(item, {
                phone: obj2.find(
                    (o) => o.p_id === item.p_id
                )
            });
        });
// console.log(obj1) would be

[
      {
        "p_id": 1,
        "name": "Peter",
        "status" : "Active",
        "phone" : {type: 'home', no: '+01 234 5678'}       
      },
      {
        "p_id": 2,
        "name": "Kane"
        "status" : "Active",
        "phone" : {type: 'home', no: '+12 345 6789'} 
      },
      {
        "p_id": 3,
        "name": "William"
        "status" : "Inactive"
        "phone" : undefined
      }

]

但这不是我想要的。我想要的是最终结果,我需要的是这些阵列之间的比较-最终结果应该是这样的:

const result = [
      {
        "p_id": 1,
        "name": "Peter",
        "status" : "Active",
        "phone" : [
           {type: 'home', no: '+01 234 5678'},
           {type: 'work', no: '+09 111 2223'}        
        ]
      },
      {
        "p_id": 2,
        "name": "Kane"
        "status" : "Active",
        "phone" : [
           {type: 'home', no: '+12 345 6789'}      
        ]
      },
      {
        "p_id": 3,
        "name": "William"
        "status" : "Inactive"
        "phone" : []
      }

]

非常感谢您的帮助,谢谢!

dbf7pr2w

dbf7pr2w1#

您可以这样做:

const users = [
  {
    p_id: 1,
    name: "Peter",
    status: "Active",
  },
  {
    p_id: 2,
    name: "Kane",
    status: "Active",
  },
  {
    p_id: 3,
    name: "William",
    status: "Inactive",
  },
];

const phoneNumbers = [
  { p_id: 1, type: "home", no: "+01 234 5678" },
  { p_id: 1, type: "work", no: "+09 111 2223" },
  { p_id: 2, type: "home", no: "+12 345 6789" },
];

const mergeArrays = (arr1, arr2) => {
  return arr1.map((obj) => {
    const numbers = arr2.filter((nums) => nums["p_id"] === obj["p_id"]);
    if (!numbers.length) {
      obj.phone = numbers;
      return obj;
    }
    obj.phone = numbers.map((num) => ({ type: num.type, no: num.no }));
    return obj;
  });
};

const result = mergeArrays(users, phoneNumbers);
console.log(result);

说明:
使用 filter 方法查找中具有相同id的所有对象 phoneNumbers 数组。然后使用 map 方法,并返回不带id的phone number对象。

4xrmg8kj

4xrmg8kj2#

而不是 Array.find 尝试 Array.filter 方法。 find 将只返回第一个元素,而 filter 将返回满足条件的所有元素:

Object.assign(item, {
    phone: obj2.filter(
       (o) => o.p_id === item.p_id
    )
 });
rqdpfwrv

rqdpfwrv3#

这里是最好的方法

// intersect can be simulated via
const intersection = new Set([...mySet1].filter(x => mySet2.has(x)))

// difference can be simulated via
const difference = new Set([...mySet1].filter(x => !mySet2.has(x)))

相关问题