简体   繁体   English

合并两个对象数组,跳过具有相同ID属性的对象

[英]Merge two array of objects with skipping objects that has same ID property

I wanna merge two arrays of objects but I want to skip the objects that has the same ID (i want to save only first object that has same id). 我想合并两个对象数组但我想跳过具有相同ID的对象(我想只保存具有相同id的第一个对象)。

One array is stored locally and the other I'm fetching users from API. 一个数组存储在本地,另一个数组从API获取用户。

const localUsers = [
    {
        "id": 1,
        "first_name": "Adam",
        "last_name": "Bent",
        "avatar": "some img url"
    },
    {
        "id": 2,
        "first_name": "New Name",
        "last_name": "New Last Name",
        "avatar": "some new img url"
    }

];

const apiUsers = [
    {
        "id": 2,
        "first_name": "Eve",
        "last_name": "Holt",
        "avatar": "some img url"
    },
    {
        "id": 3,
        "first_name": "Charles",
        "last_name": "Morris",
        "avatar": "some img url"
    }
];

I expect to get this. 我希望得到这个。 The object in apiUsers with the id: 2 is skipped , because he already exist in the localUsers array of objects. apiUsers中id为2的对象被跳过 ,因为他已经存在于localUsers对象数组中。 I want to do this to all the objects with the same id. 我想对具有相同id的所有对象执行此操作。

const mergedUsers = [
    {
        "id": 1,
        "first_name": "Adam",
        "last_name": "Bent",
        "avatar": "some img url"
    },
    {
        "id": 2,
        "first_name": "New Name",
        "last_name": "New Last Name",
        "avatar": "some new img url"
    },
    {
        "id": 3,
        "first_name": "Charles",
        "last_name": "Morris",
        "avatar": "some img url"
    }

];

Create your mergedUsers concatenating localUsers and the apiUsers that are not in localUsers already: 创建mergedUsers连接localUsers和已经不在localUsers中的apiUsers:

 const localUsers = [ { "id": 1, "first_name": "Adam", "last_name": "Bent", "avatar": "some img url" }, { "id": 2, "first_name": "New Name", "last_name": "New Last Name", "avatar": "some new img url" } ]; const apiUsers = [ { "id": 2, "first_name": "Eve", "last_name": "Holt", "avatar": "some img url" }, { "id": 3, "first_name": "Charles", "last_name": "Morris", "avatar": "some img url" } ]; const mergedUsers = localUsers.concat(apiUsers.filter(a => !localUsers.find(b => b.id === a.id))); console.log(mergedUsers); 

You could take a Map by reducing the arrays in the wanted order. 您可以通过减少所需顺序中的数组来获取Map

 const localUsers = [{ id: 1, first_name: "Adam", last_name: "Bent", avatar: "some img url" }, { id: 2, first_name: "New Name", last_name: "New Last Name", avatar: "some new img url" }], apiUsers = [{ id: 2, first_name: "Eve", last_name: "Holt", avatar: "some img url" }, { id: 3, first_name: "Charles", last_name: "Morris", avatar: "some img url" }], result = Array.from( [localUsers, apiUsers] .reduce( (m, a) => a.reduce((n, o) => n.set(o.id, n.get(o.id) || o), m), new Map ) .values() ); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

First remove all instance from apiUsers that exist in localUsers , then add that array to localUsers . 首先从apiUsers中存在的localUsers删除所有实例,然后将该数组添加到localUsers The order of the array does not matter here as not stated in the question, but easy to perform. 数组的顺序在这里并不重要,因为问题中没有说明,但易于执行。

const filteredApiUsers = apiUsers.filter(x => !localUsers.some(y => x.id === y.id));
const mergedUsers = [...localUsers, ...filteredApiUsers];

you can use this to combine the two objects with duplicates: 您可以使用它将两个对象组合在一起:

Array.prototype.push.apply(localUsers,apiUsers);

then you can remove duplicates with the new object. 然后你可以删除新对象的重复项。

reference : Merge 2 arrays of objects reference: 合并2个对象数组

  1. Filter out users from apiUserArray that are already in the localUserArray. 从apiUserArray中过滤掉已经在localUserArray中的用户。
  2. Merge both arrays. 合并两个数组。

     let distinctApiUsers = apiUsers .filter(rUser => localUsers .findIndex(lUser => lUser.id == rUser.id) == -1) let mergedArray = localUsers.concat(distinctApiUsers) 

It's easy enough to write your own comparison function: 编写自己的比较函数很容易:


    for(var i=0; i<localUsers.length; ++i) {
        var checked = 0;
        for(var j=i+1; j<apiUsers.length; ++j) {
             if(localUsers[i]['id'] === apiUsers[j]['id']){
                  apiUsers.splice(j--, 1);
             }
        }
    }
    console.log(localUsers.concat(apiUsers));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM