xxxxxxxxxx
const arr1 = [1, 2, 3];
const arr2 = [1, 3, 3];
if (arr1.length !== arr2.length) return console.log("false");
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i] === arr2[j]) {
console.log("yes match", arr1[i], arr2[j]);
continue;
}
console.log("no match", arr1[i], arr2[j]);
}
}
xxxxxxxxxx
const compareArrays = (a, b) =>
a.length === b.length &&
a.every((element, index) => element === b[index]);
let array1 = [11, 22, 33];
let array2 = [21, 22, 23];
let array3 = [11, 22, 33];
console.log(compareArrays(array1, array2)); //false
console.log(compareArrays(array1, array3)); //true
xxxxxxxxxx
Array.prototype.equals = function(arr2) {
return (
this.length === arr2.length &&
this.every((value, index) => value === arr2[index])
);
};
[1, 2, 3].equals([1, 2, 3]); // true
[1, 2, 3].equals([3, 6, 4, 2]); // false
xxxxxxxxxx
const compareArrays = (a, b) => {
return JSON.stringify(a) === JSON.stringify(b);
};
let array1 = [11, 22, 33];
let array2 = [21, 22, 23];
let array3 = [11, 22, 33];
console.log(compareArrays(array1, array2)); //false
console.log(compareArrays(array1, array3)); //true
xxxxxxxxxx
function arraysAreIdentical(arr1, arr2){
if (arr1.length !== arr2.length) return false;
for (var i = 0, len = arr1.length; i < len; i++){
if (arr1[i] !== arr2[i]){
return false;
}
}
return true;
}
xxxxxxxxxx
let arr1 = [1, 4, 7, 4, 2, 3];
let arr2 = [1, 2, 3, 4, 7, 18];
const is_same = arr1.length == arr2.length &&
(arr1.every((currElem)=>{
if(arr2.indexOf(currElem)> -1){
return (currElem == arr2[arr2.indexOf(currElem)]);
}return false
})
)
console.log(is_same)
xxxxxxxxxx
const equals = (a, b) => JSON.stringify(a) === JSON.stringify(b);
let arr1 = ['1','2'];
let arr2 = ['1','2'];
equals(arr1,arr2)//this return false , if not equal then its return false
xxxxxxxxxx
// THE PROBLEM:
const firstArray = ["cookies", "milk", "chocolate"]
const secondArray = ["cookies", "milk", "chocolate"]
console.log(firstArray == secondArray) // always returns FALSE
// THE SOLUTION:
if(JSON.stringify(firstArray) === JSON.stringify(secondArray)){
console.log("firstArray is the same as the secondArray")
} else {
console.log("firstArray is different from the secondArray")
}
xxxxxxxxxx
// Warn if overriding existing method
if(Array.prototype.equals)
console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
// if the other array is a falsy value, return
if (!array)
return false;
// compare lengths - can save a lot of time
if (this.length != array.length)
return false;
for (var i = 0, l=this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});