如何比较两个对象并通过javascript中的值删除重复的对象

时间:2018-10-29 07:39:36

标签: javascript reactjs lodash

我有两个对象,我想比较这些对象,并按值删除重复的对象,并显示其余对象。我尝试了lodash省略但不起作用。

    let users = [
  {value: "6qcuXSLWAnVospWkugWa", label: "Karthi"},
  {value: "Qk8pc08WmcacM7BqFxow", label: "Karthi"},
  {value: "evLkHAkhNZ9qqeYc1tDn", label: "Sankar"},
  {value: "WE6kIW8sGkEuMRXQgQPT", label: "User8"},
  {value: "km2jHXDQgPXdBY4jq6dG", label: "User50"},
  {value: "ObQhbfH3YroudLVz5YkY", label: "User10"}
]

let selectedUsers = [
{value: "6qcuXSLWAnVospWkugWa", label: "Karthi"},
{value: "Qk8pc08WmcacM7BqFxow", label: "Karthi"},
{value: "evLkHAkhNZ9qqeYc1tDn", label: "Sankar"},
{value: "WE6kIW8sGkEuMRXQgQPT", label: "User8"},
{value: "km2jHXDQgPXdBY4jq6dG", label: "User50"},
{value: "ObQhbfH3YroudLVz5YkY", label: "User10"},
{value: "L9FW5oXDlDmzhWxhGEdu", label: "Govind"}
]

不使用多个循环就可以删除重复项

5 个答案:

答案 0 :(得分:1)

var userObjectOfObjects = users.reduce(
        (acc, o) => {
            acc[o.value] = o;
            return acc;
        }, {} )

var selectUsersObject = selectedUsers.reduce(
    (acc, o) => {
        if(userObjectOfObjects[o.value]){
            acc.duplicates.push(o)
            return acc;
        }
        else {
            acc.objectWithoutDuplicates.push(o)
        }
        acc.uniqueList[o.value] = o;
        return acc;
    }, { uniqueList: {}, objectWithoutDuplicates:[], duplicates: [] } )


selectUsersObject.uniqueList = {
    ...selectUsersObject.uniqueList,
    ...userObjectOfObjects
}

var uniqueList = Object.values(selectUsersObject.uniqueList)
var duplicates = selectUsersObject.duplicates
var noRepetition = selectUsersObject.objectWithoutDuplicates

答案 1 :(得分:0)

let result = _.unionBy(users, selectedUsers, 'value');

答案 2 :(得分:0)

您可以使用differenceWithisEqualselectedUsers获取唯一值。

let users = [{value: "6qcuXSLWAnVospWkugWa", label: "Karthi"}, {value: "Qk8pc08WmcacM7BqFxow", label: "Karthi"}, {value: "evLkHAkhNZ9qqeYc1tDn", label: "Sankar"}, {value: "WE6kIW8sGkEuMRXQgQPT", label: "User8"}, {value: "km2jHXDQgPXdBY4jq6dG", label:
"User50"}, {value: "ObQhbfH3YroudLVz5YkY", label: "User10"}],
    selectedUsers = [{value: "6qcuXSLWAnVospWkugWa", label: "Karthi"}, {value: "Qk8pc08WmcacM7BqFxow", label: "Karthi"}, {value: "evLkHAkhNZ9qqeYc1tDn", label: "Sankar"}, {value: "WE6kIW8sGkEuMRXQgQPT",
label: "User8"}, {value: "km2jHXDQgPXdBY4jq6dG", label: "User50"}, {value: "ObQhbfH3YroudLVz5YkY", label: "User10"}, {value: "L9FW5oXDlDmzhWxhGEdu", label: "Govind"}],
    result = _.differenceWith(selectedUsers, users, _.isEqual);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

答案 3 :(得分:0)

任何解决方案都将需要多个循环。是您编写了多个循环,或者您编写的速记代码在内部使用了多个循环。

这种单线应该为您工作

const arrWithoutDup = selectedUsers.filter(sUser => users.filter(user => sUser.value === user.value && sUser.value === user.value).length === 0);

答案 4 :(得分:0)

假设要复制的对象,valuelabel的值应该相同。

您可以尝试使用具有线性复杂度的简单Javascript。首先,将users数组转换为对象,然后在第二个数组上使用Array.filter,过滤掉重复的对象。

let users = [{value: "6qcuXSLWAnVospWkugWa", label: "Karthi"},{value: "Qk8pc08WmcacM7BqFxow", label: "Karthi"},{value: "evLkHAkhNZ9qqeYc1tDn", label: "Sankar"},{value: "WE6kIW8sGkEuMRXQgQPT", label: "User8"},{value: "km2jHXDQgPXdBY4jq6dG", label: "User50"},{value: "ObQhbfH3YroudLVz5YkY", label: "User10"}];
let selectedUsers = [{value: "6qcuXSLWAnVospWkugWa", label: "Karthi"},{value: "Qk8pc08WmcacM7BqFxow", label: "Karthi"},{value: "evLkHAkhNZ9qqeYc1tDn", label: "Sankar"},{value: "WE6kIW8sGkEuMRXQgQPT", label: "User8"},{value: "km2jHXDQgPXdBY4jq6dG", label: "User50"},{value: "ObQhbfH3YroudLVz5YkY", label: "User10"},{value: "L9FW5oXDlDmzhWxhGEdu", label: "Govind"}];

let uersObj = users.reduce((a,c) => Object.assign(a, {[c.value]:c.label}), {});
let result = selectedUsers.filter(v => !uersObj[v.value] || uersObj[v.value] !== v.label);
console.log(result);