DekGenius.com
JAVASCRIPT
check if 2 arrays are equal javascript
const a = [1, 2, 3];
const b = [4, 5, 6];
const c = [1, 2, 3];
function arrayEquals(a, b) {
return Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index]);
}
arrayEquals(a, b); // false
arrayEquals(a, c); // true
check if array has same values javascript
const allEqual = arr => arr.every(v => v === arr[0]);
allEqual([1,1,1,1]); // true
Easy Way to Check if 2 Arrays are Equal in JavaScript
const d = [1,2,4,5,6];
const c = [1,2,4,5,6];
if (JSON.stringify(d) === JSON.stringify(c)) console.log('Equal!');
javascript check if two arrays contain same values
const a = ['Left', 'Right'];
const b = ['Right', 'Left'];
// true if a and b contain the same values
// false otherwise
const c = a.sort().join(',') === b.sort().join(',');
js check if all array values are the same
const allEqual = arr => arr.every( v => v === arr[0] )
allEqual( [1,1,1,1] ) // true
check if two array contains the same values js, array
const areEqual = (arr1, arr2) =>
arr1.sort().join(',') === arr2.sort().join(',');
const arr1 = [1, 2, 3, 4];
const arr2 = [3, 4, 1, 2];
const arr3 = [1, 2, 3];
console.log(areEqual(arr1, arr2)); // true
console.log(areEqual(arr1, arr3)); // false
© 2022 Copyright:
DekGenius.com