【问题标题】:javascript - concat two arrays but with identical objectsjavascript - 连接两个数组但具有相同的对象
【发布时间】:2022-12-04 11:11:24
【问题描述】:

I have this array of all users:

let allUsers = [
    {
        id: 1,
        name: \"Mike\"
    },
    {
        id: 2,
        name: \"John\"
    },
    {
        id: 3,
        name: \"Kim\"
    },
    {
        id: 4,
        name: \"Mike\"
    }
];

Now I have array of batches. Each batch has its own users array.

const userBatches = [
    {   
        \"batchId\": 1,
        \"users\": [
            {
                id: 5,
                name: \"Max\"
            },
            {
                id: 2,
                name: \"Simon\"
            }
        ]
    },
    {   
        \"batchId\": 2,
        \"users\": [
            {
                id: 6,
                name: \"Max\"
            },
            {
                id: 7,
                name: \"Conor\"
            }
        ]
    },
    {   
        \"batchId\": 3,
        \"users\": [
            {
                id: 3,
                name: \"Norman\"
            }
        ]
    }
]

Here I want to push only those users that does not exists in allUsers array. (on the basis of user id not name)

In simple words allUsers should contain the unique users. No duplicates.

Expected response of allUsers:

[
    {
        id: 1,
        name: \"Mike\"
    },
    {
        id: 2,
        name: \"John\"
    },
    {
        id: 3,
        name: \"Kim\"
    },
    {
        id: 4,
        name: \"Mike\"
    },
    {
        id: 5,
        name: \"Max\"
    },
    {
        id: 6,
        name: \"Max\"
    },
    {
        id: 7
        name: \"Conor\"
    }
]

Here is the attached code snippet:

let allUsers = [
    {
        id: 1,
        name: \"Mike\"
    },
    {
        id: 2,
        name: \"John\"
    },
    {
        id: 3,
        name: \"Kim\"
    },
    {
        id: 4,
        name: \"Mike\"
    }
];

const userBatches = [
    {   
        \"batchId\": 1,
        \"users\": [
            {
                id: 5,
                name: \"Max\"
            },
            {
                id: 2,
                name: \"Simon\"
            }
        ]
    },
    {   
        \"batchId\": 2,
        \"users\": [
            {
                id: 6,
                name: \"Max\"
            },
            {
                id: 7,
                name: \"Conor\"
            }
        ]
    },
    {   
        \"batchId\": 3,
        \"users\": [
            {
                id: 3,
                name: \"Norman\"
            }
        ]
    }
];

userBatches.forEach((batch) => {
    
  console.log(batch)
    // if batch users does not exists allUsers array then add these users to allUsers array 
});
  • You pretty much have solved it yourself with your comment after the .log. JS array some and you got it.
  • yeah I know the concept but I am unable to apply the better approach here, I need a clean and optimized way to achieve this.

标签: javascript arrays typescript


【解决方案1】:
【解决方案2】:
猜你喜欢
  • 2022-11-18
  • 2018-12-19
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
  • 2018-05-22
相关资源
最近更新 更多