Created
October 18, 2016 08:43
-
-
Save Aleksey-Danchin/8ff0be6c7d5ce37073c878c34b170cc8 to your computer and use it in GitHub Desktop.
Проверка массивов на эквивалентность содержимого.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.isEqual = function (...arrays) { | |
if (arrays.length == 0) return false; | |
if (arrays.length == 1) return true; | |
const ctrl = arrays[0] | |
, length = ctrl.length; | |
for (let i = 1; i < arrays.length; i++) { | |
const array = arrays[i]; | |
if (length != array.length) | |
return false; | |
for (let j = 1; j < length; j++) | |
if (!array.includes(ctrl[i]) || | |
!ctrl.includes(array[i]) | |
) return false; | |
} | |
return true; | |
} | |
console.log(Array.isEqual([], [], [])); // true | |
console.log(Array.isEqual([1, 2, 3], [2, 1, 3], [3, 2, 1])); // true | |
console.log(Array.isEqual([1, 2, 3], [2, 1, 3], [3, 2, '1'])); // false | |
console.log(Array.isEqual([1, 2, 3], [2, 1, 3], [3, 2, 1, 4])); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment