Skip to content

Instantly share code, notes, and snippets.

@Aleksey-Danchin
Created October 18, 2016 08:43
Show Gist options
  • Save Aleksey-Danchin/8ff0be6c7d5ce37073c878c34b170cc8 to your computer and use it in GitHub Desktop.
Save Aleksey-Danchin/8ff0be6c7d5ce37073c878c34b170cc8 to your computer and use it in GitHub Desktop.
Проверка массивов на эквивалентность содержимого.
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