let arr1 = [{ countryCode: "ITA", index: 2, name: "Italy"}, { countryCode: "NLD", index: 1, name: "Netherlands"}];
let arr2 = [{ countryCode: "NLD", index: 1, name: "Netherlands"}, { countryCode: "BEL", index: 3, name: "Belgium"}];
I want it to return the symmetric difference, so it should return:
[{ countryCode: "ITA", index: 2, name: "Italy"}, {countryCode: "BEL", index: 3, name: "Belgium"}]
How can I accomplish this in Javascript? I tried to do the following:
let difference = arr1
.filter(x => !arr2.includes(x))
.concat(arr2.filter(x => !arr1.includes(x)));
But this doesn't seem to work for arrays with objects in them.
question from:
https://stackoverflow.com/questions/65835785/symmetric-difference-for-arrays-with-objects-javascript 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…