Compare two Javascript Arrays and remove Duplicates
array1 = array1.filter(function(val) {
return array2.indexOf(val) == -1;
});
Or, with the availability of ES6:
array1 = array1.filter(val => !array2.includes(val));
filter() reference here
indexOf() reference here
includes() reference here
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);
Compare two javascript arrays and remove duplicate entries
Realistically you want to do something like:
[a, b, c] - [a, b]
Which would give you c
. You can achieve this using .some
, which allows you to "customize" the includes functionality.
See example below:
const arr1 = [ {"member_name":"Test1","item":"Sword"}, {"member_name":"Test2","item":"Sword"}];
const arr2 = [ {"member_name":"Test1","item":"Sword"}, {"member_name":"Test2","item":"Sword"}, {"member_name":"Test1","item":"Shield"}];
const res = arr2.filter(({member_name:a, item:x}) => !arr1.some(({member_name:b, item:y}) => a === b && x === y));console.log(res);
How to compare two arrays and remove duplicate objects by complete iteration
You could use a Set
for the given id
and filter selectedRows
with the set.
var deSelectedRows = [{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04"}], selectedRows = [{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04"}], ids = new Set(deSelectedRows.map(({ id }) => id));
selectedRows = selectedRows.filter(({ id }) => !ids.has(id));
console.log(selectedRows);
JS compare two arrays with objects and remove duplicated by property name ES6 way
Array.includes
won't work because in javascript {} !== {}
. You'll need another way like Array.every
to check that every object in the other array doesn't have the same value of the property item
as the current object. Also, you need to do both arr1.filter(...)
and arr2.filter(...)
and concat
the results:
arr3 = [].concat(
arr1.filter(obj1 => arr2.every(obj2 => obj1.item !== obj2.item)),
arr2.filter(obj2 => arr1.every(obj1 => obj2.item !== obj1.item))
);
Example:
let arr1 = [{ item:"apple", description: "lorem"},{ item:"peach", description: "impsum"}];
let arr2 = [{ item:"apple", description: "dolor"},{ item:"grape", description: "enum"}];
let arr3 = [].concat( arr1.filter(obj1 => arr2.every(obj2 => obj1.item !== obj2.item)), arr2.filter(obj2 => arr1.every(obj1 => obj2.item !== obj1.item)));
console.log(arr3);
Compare two objects in an array, delete the duplicate data, and combine the different data?
You could collect all with an object with combined keys.
const
array = [{ id: 1, name: 'test', condition: 'left' }, { id: 2, name: 'example', condition: 'left' }, { id: 1, name: 'test', condition: 'right' }, { id: 3, name: 'foobar', condition: 'right' }],
keys = ['id', 'name'],
result = Object.values(array.reduce((r, o) => {
const key = keys.map(k => o[k]).join('|');
if (r[key]) r[key].condition = [].concat(r[key].condition, o.condition);
else r[key] = { ...o };
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Compare two array and remove if same value using javascript
Here is one more using reduce
var arr1 = [1, 2, 3, 4, 5];var arr2 = [1, 3, 5, 7, 9];
var result = arr1.reduce(function (prev, value) {
var isDuplicate = false; for (var i = 0; i < arr2.length; i++) { if (value == arr2[i]) { isDuplicate = true; break; } } if (!isDuplicate) { prev.push(value); } return prev; }, []);
alert(JSON.stringify(result.concat(arr2)));
Related Topics
Anything Similar in JavaScript to Ruby's #{Value} (String Interpolation)
Template Language That Works on Both Server and Client
How to Parse JavaScript Using Nokogiri and Ruby
How to Disable Tree Shaking in Rollupjs
How to See Ping Pong in Web Socket in iOS
Understanding Service Worker Scope
How to Display PDF (Blob) on iOS Sent from My Angularjs App
Ckeditor 4: Uncaught Typeerror: Cannot Read Property 'Langentries' of Null
How to Listen for Keyboard Open/Close in JavaScript/Sencha
How to Detect Scroll Position of Page Using Jquery
How to Load Binary Image Data Using JavaScript and Xmlhttprequest
Display Spinner While Datatables Table Loads Ruby on Rails
Can Jquery .Keypress() Detect More Than One Key at the Same Time
Wkwebview - Complex Communication Between JavaScript & Native Code
Reactjs - Lifting State Up VS Keeping a Local State
Passing Array of Objects from Js to Rails
Why Is the Ecc-Dh Symmetric Key of This Site Different from Openssl