I want position of the array is to be also same and value also same.
var array1 = [4,8,9,10];
var array2 = [4,8,9,10];
I tried like this
var array3 = array1 === array2 // returns false
I want position of the array is to be also same and value also same.
var array1 = [4,8,9,10];
var array2 = [4,8,9,10];
I tried like this
var array3 = array1 === array2 // returns false
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];
});
(array1.length === array2.length)
. ===
instead of ==
.(array1.length == array2.length) && array1.every(el => array2.includes(el))
A less robust approach, but it works.
a = [2, 4, 5].toString();
b = [2, 4, 5].toString();
console.log(a===b);
sort()
and this should then solve the op's problem.[2, 4, 5].toString()
and b = ["2,4", 5].toString()
.var array3 = array1 === array2
That will compare whether array1 and array2 are the same array object in memory, which is not what you want.
In order to do what you want, you'll need to check whether the two arrays have the same length, and that each member in each index is identical.
Assuming your array is filled with primitives—numbers and or strings—something like this should do
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;
}
A more modern version:
function arraysEqual(a, b) {
a = Array.isArray(a) ? a : [];
b = Array.isArray(b) ? b : [];
return a.length === b.length && a.every((el, ix) => el === b[ix]);
}
Coercing non-array arguments to empty arrays stops a.every()
from exploding.
If you just want to see if the arrays have the same set of elements then you can use Array.includes()
:
function arraysContainSame(a, b) {
a = Array.isArray(a) ? a : [];
b = Array.isArray(b) ? b : [];
return a.length === b.length && a.every(el => b.includes(el));
}
arraysEqual(23, "")
arraysEqual
should be allowed to receive non-array inputs at all. That said, it's still good to be aware of the aforementioned "gotcha".arraysContainSame([1,1,2], [1,2,2])
. This is rarely what you want, you probably want to count multiplicity.You could try this simple approach
var array1 = [4,8,9,10];
var array2 = [4,8,9,10];
console.log(array1.join('|'));
console.log(array2.join('|'));
if (array1.join('|') === array2.join('|')) {
console.log('The arrays are equal.');
} else {
console.log('The arrays are NOT equal.');
}
array1 = [[1,2],[3,4],[5,6],[7,8]];
array2 = [[1,2],[3,4],[5,6],[7,8]];
console.log(array1.join('|'));
console.log(array2.join('|'));
if (array1.join('|') === array2.join('|')) {
console.log('The arrays are equal.');
} else {
console.log('The arrays are NOT equal.');
}
If the position of the values are not important you could sort the arrays first.
if (array1.sort().join('|') === array2.sort().join('|')) {
console.log('The arrays are equal.');
} else {
console.log('The arrays are NOT equal.');
}
[4,8,9,10]
and the second is ["4|8",9,10]
for example.Here goes the code. Which is able to compare arrays by any position.
var array1 = [4,8,10,9];
var array2 = [10,8,9,4];
var is_same = array1.length == array2.length && array1.every(function(element, index) {
//return element === array2[index];
if(array2.indexOf(element)>-1){
return element = array2[array2.indexOf(element)];
}
});
console.log(is_same);
Try doing like this: array1.compare(array2)=true
Array.prototype.compare = 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].compare(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;
}
Array.prototype
is a built-in prototype object for type Arrays and this will cause inconsistency while working in a team.