Using jQuery to compare two arrays of Javascript objects
There is an easy way...
$(arr1).not(arr2).length === 0 && $(arr2).not(arr1).length === 0
If the above returns true, both the arrays are same even if the elements are in different order. NOTE: This works only for jquery versions < 3.0.0 when using JSON objects
How to Compare two Arrays are Equal using Javascript?
You could use Array.prototype.every().(A polyfill is needed for IE < 9 and other old browsers.)
var array1 = [4,8,9,10];
var array2 = [4,8,9,10];
var is_same = (array1.length == array2.length) && array1.every(function(element, index) {
return element === array2[index];
});
THE WORKING DEMO. Comparing two arrays in jquery
Regarding your comment, here is a solution:
with jQuery:
$.each( a, function( key, value ) {
var index = $.inArray( value, b );
if( index != -1 ) {
console.log( index );
}
});
without jQuery:a.forEach( function( value ) {
if( b.indexOf( value ) != -1 ) {
console.log( b.indexOf( value ) );
}
});
How to get the difference between two arrays of objects in JavaScript
Using only native JS, something like this will work:
const a = [{ value:"0", display:"Jamsheer" }, { value:"1", display:"Muhammed" }, { value:"2", display:"Ravi" }, { value:"3", display:"Ajmal" }, { value:"4", display:"Ryan" }];
const b = [{ value:"0", display:"Jamsheer", $$hashKey:"008" }, { value:"1", display:"Muhammed", $$hashKey:"009" }, { value:"2", display:"Ravi", $$hashKey:"00A" }, { value:"3", display:"Ajmal", $$hashKey:"00B" }];
// A comparer used to determine if two entries are equal.
const isSameUser = (a, b) => a.value === b.value && a.display === b.display;
// Get items that only occur in the left array,
// using the compareFunction to determine equality.
const onlyInLeft = (left, right, compareFunction) =>
left.filter(leftValue =>
!right.some(rightValue =>
compareFunction(leftValue, rightValue)));
const onlyInA = onlyInLeft(a, b, isSameUser);
const onlyInB = onlyInLeft(b, a, isSameUser);
const result = [...onlyInA, ...onlyInB];
console.log(result);
Compare Two arrays for equality if they have nested objects in Jquery
You can do it without jQuery - change them to a string by using globally available JSON.stringify
method, then the comparison will be easy:
JSON.stringify(arr1) === JSON.stringify(arr2);
This is kind of a hack. But it does work well. And in the era when Angular framework is checking it's injections by running toString()
on its functions and then regexping the attributes (oh yes it does), I think this is just an effective solution ;) Compare two objects in jQuery and get the difference
This isn't a good use of jQuery, but here is some vanilla javascript that does what you want.
function objDiff(array1, array2) {
var resultArray = []
array2.forEach(function(destObj) {
var check = array1.some(function(origObj) {
if(origObj.ID == destObj.ID) return true
})
if(!check) {
destObj.desc = 'missing in source'
resultArray.push(destObj)
}
})
array1.forEach(function(origObj) {
var check = array2.some(function(destObj) {
if(origObj.ID == destObj.ID) return true
})
if(!check) {
origObj.desc = 'missing in destination'
resultArray.push(origObj)
}
})
return resultArray
}
https://jsfiddle.net/9gaxsLbz/1/
Related Topics
How to Parse a Date in Format "Yyyymmdd" in JavaScript
How to Compare 2 Functions in JavaScript
How to Update a Specific Index from the Array in Firestore
JavaScript Object Literals Syntax Error
Merge Duplicate Objects in Array of Objects
Promises for Promises That Are Yet to Be Created Without Using the Deferred [Anti]Pattern
Why Threre Is No Way to Download File Using Ajax Request
Typeerror: Cannot Read Property 'Setstate' of Undefined
Adding Csrftoken to Ajax Request
Render Object Properties in React
Read/Write Bytes of Float in Js
How to Make a Directive Update Ng-Model on Jquery on Event
Angularjs Communication Between Directives
Unchecked Runtime.Lasterror While Running Tabs.Executescript