比较2个对象数组并删除重复项

时间:2019-01-11 07:31:08

标签: javascript arrays

我在JavaScript中有2个对象数组,想比较和合并内容,并按ID对结果进行排序。具体来说,排序后的结果数组应包含第1个数组中的所有对象,以及第2个数组中ID不在第1个中的所有对象。

以下代码似乎有效(减去排序)。但是必须有一种更好,更简洁的方法来实现此目的,尤其是ES6的功能。我认为使用Set是一种方法,但不确定如何实现。

kubectl apply -f myk8sfiles.yml

7 个答案:

答案 0 :(得分:5)

一个复杂度为O(N)的选项是在Set中的id中创建cars1,然后散布cars1和过滤后的{{1 }}到输出数组中,过滤器测试集合cars2中迭代的汽车中的id是否包含在Set中:

cars2

也要var cars1 = [ {id: 2, make: "Honda", model: "Civic", year: 2001}, {id: 1, make: "Ford", model: "F150", year: 2002}, {id: 3, make: "Chevy", model: "Tahoe", year: 2003}, ]; var cars2 = [ {id: 3, make: "Kia", model: "Optima", year: 2001}, {id: 4, make: "Nissan", model: "Sentra", year: 1982}, {id: 2, make: "Toyota", model: "Corolla", year: 1980}, ]; const cars1IDs = new Set(cars1.map(({ id }) => id)); const combined = [ ...cars1, ...cars2.filter(({ id }) => !cars1IDs.has(id)) ]; console.log(combined);

sort

combined.sort(({ id: aId }, {id: bId }) => aId - bId);

答案 1 :(得分:2)

合并两个数组,将每个数组元素及其ids放入地图中,然后根据地图值创建数组。

var cars1 = [
    {id: 2, make: "Honda", model: "Civic", year: 2001},
    {id: 1, make: "Ford",  model: "F150",  year: 2002},
    {id: 3, make: "Chevy", model: "Tahoe", year: 2003},
];

var cars2 = [
    {id: 3, make: "Kia",    model: "Optima",  year: 2001},
    {id: 4, make: "Nissan", model: "Sentra",  year: 1982},
    {id: 2, make: "Toyota", model: "Corolla", year: 1980},
];

cars = cars1.concat(cars2);
let foo = new Map();
for(const c of cars){
  foo.set(c.id, c);
}
let final = [...foo.values()]
console.log(final)

答案 2 :(得分:2)

您可以乘Map并先将其中的物品映射为地图或实际的汽车。

var cars1 = [{ id: 2, make: "Honda", model: "Civic", year: 2001 }, { id: 1, make: "Ford",  model: "F150",  year: 2002 }, { id: 3, make: "Chevy", model: "Tahoe", year: 2003 }],
    cars2 = [{ id: 3, make: "Kia",    model: "Optima",  year: 2001 }, { id: 4, make: "Nissan", model: "Sentra",  year: 1982 }, { id: 2, make: "Toyota", model: "Corolla", year: 1980 }],
    result = Array
        .from(
            [...cars1, ...cars2]
                .reduce((m, c) => m.set(c.id, m.get(c.id) || c), new Map)
                .values()
        )
        .sort((a, b) => a.id - b.id);

console.log(result);

答案 3 :(得分:0)

您可以使用1 name1 country1 1 name2 country2 1 name1 country1 2 name2 country2 3 name1 country1 4 name2 country2 Id 1 name name1 country country1

concat

答案 4 :(得分:0)

您可以将Object.values().concat().reduce()一起使用:

let cars1 = [
    {id: 2, make: "Honda", model: "Civic", year: 2001},
    {id: 1, make: "Ford",  model: "F150",  year: 2002},
    {id: 3, make: "Chevy", model: "Tahoe", year: 2003},
];

let cars2 = [
    {id: 3, make: "Kia",    model: "Optima",  year: 2001},
    {id: 4, make: "Nissan", model: "Sentra",  year: 1982},
    {id: 2, make: "Toyota", model: "Corolla", year: 1980},
];

let merge = (arr1, arr2) => Object.values(
    arr1.concat(arr2).reduce((r, c) => (r[c.id] = r[c.id] || c, r), {})
).sort((a, b) => a.id - b.id);

console.log(merge(cars1, cars2));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 5 :(得分:0)

假设ID应该是唯一的,这应该可以工作:

var union = (arr1, arr2) => 
{
    var result = arr1.slice(0);
    arr2.forEach((el) => 
    { 
        if (getIndexByAttribute(arr1, 'id', el.id) < 0)
            result .push(el); 
    });
    return result;
};

var getIndexByAttribute = (array, attr, value) => {
    for(var i = 0; i < array.length; i += 1) {
        if(array[i][attr] === value) {
            return i;
        }
    }
    return -1;
}

但是对于您当前的示例cars1cars2,您可能需要对象预备。请参阅Object comparison in JavaScript [duplicate]

答案 6 :(得分:0)

一种方法是将concat()cars2的id不在cars1上的元素一起使用,可以使用find()进行检查。最后sort()得到的数组:

var cars1 = [
    {id: 2, make: "Honda", model: "Civic", year: 2001},
    {id: 1, make: "Ford",  model: "F150",  year: 2002},
    {id: 3, make: "Chevy", model: "Tahoe", year: 2003},
];
    
var cars2 = [
    {id: 3, make: "Kia",    model: "Optima",  year: 2001},
    {id: 4, make: "Nissan", model: "Sentra",  year: 1982},
    {id: 2, make: "Toyota", model: "Corolla", year: 1980},
];

let res = cars1.concat(
    cars2.filter(({id}) => !cars1.find(x => x.id === id))
).sort((a, b) => a.id - b.id);

console.log(res);