check the difference between two arrays of objects in javascript
.Filter()
and .some()
functions will do the trick
var b1 = [ { id: 0, name: 'john' }, { id: 1, name: 'mary' }, { id: 2, name: 'pablo' }, { id: 3, name: 'escobar' } ];
var b2 = [ { id: 0, name: 'john' }, { id: 1, name: 'mary' }];
var res = b1.filter(item1 => !b2.some(item2 => (item2.id === item1.id && item2.name === item1.name)))
console.log(res);
Getting difference between 2 arrays of objects in JavaScript
You were almost there, use id
instead of x
existingBillPlans.filter(item => !billPlans.some(other => item.id == other.id));
Demo
var existingBillPlans = [{ id: 2, fees: 10000, name: 'Yearly Plan', cycle: 12 }, { id: 3, fees: 1500, name: 'Two Months Plan', cycle: 2 }, { id: 4, fees: 2500, name: 'Three Months Plan', cycle: 3 }, { id: 5, fees: 3000, name: 'Four Months Plan', cycle: 4 }, { id: 181, fees: 4000, name: 'Five Months Plan', cycle: 5 }, { id: 182, fees: 5000, name: 'Six Months Plan', cycle: 6 }, { id: 183, fees: 6000, name: 'Seven Months Plan', cycle: 7 }]var billPlans = [{ id: 2, fees: 10000, name: 'Yearly Plan', cycle: 12 }, { id: 3, fees: 1500, name: 'Two Months Plan', cycle: 2 }, { id: 4, fees: 2500, name: 'Three Months Plan', cycle: 3 }]var output = existingBillPlans.filter(item => !billPlans.some(other => item.id == other.id));
console.log(output);
Comparing two arrays of objects, and exclude the elements who match values into new array in JS
Just using the Array iteration methods built into JS is fine for this:
var result1 = [ {id:1, name:'Sandra', type:'user', username:'sandra'}, {id:2, name:'John', type:'admin', username:'johnny2'}, {id:3, name:'Peter', type:'user', username:'pete'}, {id:4, name:'Bobby', type:'user', username:'be_bob'}];
var result2 = [ {id:2, name:'John', email:'johnny@example.com'}, {id:4, name:'Bobby', email:'bobby@example.com'}];
var props = ['id', 'name'];
var result = result1.filter(function(o1){ // filter out (!) items in result2 return !result2.some(function(o2){ return o1.id === o2.id; // assumes unique id });}).map(function(o){ // use reduce to make objects with only the required properties // and map to apply this to the filtered array as a whole return props.reduce(function(newo, name){ newo[name] = o[name]; return newo; }, {});});
document.body.innerHTML = '<pre>' + JSON.stringify(result, null, 4) + '</pre>';
Difference of two arrays of objects with lodash
You can use xorBy
to indicate a property used for comparison:
const A1 = [{id: 1, nome: "Ruan"}, {id: 2, nome: "Gleison"}]
const A2 = [{id: 2, nome: "Gleison"}, {id: 3, nome: "Geraldo"}]
const results = _.xorBy(A1, A2, 'id'); // or 'nome'
console.log(results)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
Comparing Arrays of Objects in JavaScript
EDIT: You cannot overload operators in current, common browser-based implementations of JavaScript interpreters.
To answer the original question, one way you could do this, and mind you, this is a bit of a hack, simply serialize the two arrays to JSON and then compare the two JSON strings. That would simply tell you if the arrays are different, obviously you could do this to each of the objects within the arrays as well to see which ones were different.
Another option is to use a library which has some nice facilities for comparing objects - I use and recommend MochiKit.
EDIT: The answer kamens gave deserves consideration as well, since a single function to compare two given objects would be much smaller than any library to do what I suggest (although my suggestion would certainly work well enough).
Here is a naïve implemenation that may do just enough for you - be aware that there are potential problems with this implementation:
function objectsAreSame(x, y) {
var objectsAreSame = true;
for(var propertyName in x) {
if(x[propertyName] !== y[propertyName]) {
objectsAreSame = false;
break;
}
}
return objectsAreSame;
}
The assumption is that both objects have the same exact list of properties.
Oh, and it is probably obvious that, for better or worse, I belong to the only-one-return-point camp. :)
Related Topics
Disable Click Outside of Bootstrap Modal Area to Close Modal
Jquery - Ajax Returning Error 500 on Some Posts
How to Instantiate a JavaScript Class in Another Js File
How to Get 30 Days Prior to Current Date
How to Find Nearest Location Using Latitude and Longitude from a Json Data
Filter Json Object to String Array by Key Value
Open a New Tab on Button Click in Angularjs
What Is the Purpose of the Dollar Sign in JavaScript
How to Prevent Invalid Characters from Being Typed into Input Fields
React Typescript Is Not Assignable to Parameter of Type
How to Completely Remove Ionic and Cordova Installation from Mac
How to Push Array to Json Object in Angular
How to Make Select Option Disable
Setting Cross-Domain Cookies in Safari
How to Create a Dynamic Jquery Data Table With Json Data and Ajax
Increase and Decrease Number With JavaScript and Two Buttons Up and Down