Compare 2 Arrays of Objects and Remove Duplicates
One option with O(N)
complexity would be to make a Set
of the id
s in cars1
, then spread cars1
and a filtered cars2
into the ouput array, with the filter testing whether the id
in the car being iterated over in cars2
is included in the Set:
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);
To sort
as well:
combined.sort(({ id: aId }, {id: bId }) => aId - bId);
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))
];
combined.sort(({ id: aId }, {id: bId }) => aId - bId);
console.log(combined);
You could take a Map
and take the item of them map first or the actual car.
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);
Merge two arrays, put each array element in a map with their ids
and then create array from the map values.
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)
You could use concat
, filter
and 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}, ];
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}, ];
// Resulting cars1 contains all cars from cars1 plus unique cars from cars2
let ids = cars1.map(c => c.id);
cars1 = cars1.concat(cars2.filter(({id}) => !ids.includes(id)))
console.log(cars1);