@writetome51/arrays-match
v2.0.3
Published
Takes 2 arrays as arguments, and if they match, returns true
Downloads
19
Maintainers
Readme
arraysMatch(array1, array2): boolean
If array1
and array2
match, returns true.
It automatically handles checking nested arrays.
How the matching is done:
If (array1 === array2)
, returns true.
Else, it tries element-by-element matching:
if array1[i] === array2[i]
for every i
in array1
and array2
, it's a match.
If array1[i]
and array2[i]
are both arrays of equal length, they're passed
into a recursive function call.
Examples
arraysMatch([], []); // true
arraysMatch(['h', 'j'], ['h', 'j']); // true
arraysMatch(['h', 'j'], ['h', 'j', 'k']); // false
arraysMatch([1, 2, [3]], [1, 2, [3]]); // true
let obj = {prop: 1};
arraysMatch([obj], [{prop:1}]); // false
arraysMatch([obj], [obj]); // true
let obj2 = obj;
arraysMatch([obj], [obj2]); // true
Installation
npm i @writetome51/arrays-match
Loading
import {arraysMatch} from '@writetome51/arrays-match';