Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/writetome51/arrays-match

An array-comparison function
https://github.com/writetome51/arrays-match

arrays comparison comparison-tool javascript match matching

Last synced: 3 days ago
JSON representation

An array-comparison function

Awesome Lists containing this project

README

        

[![npm version](https://badge.fury.io/js/%40writetome51%2Farrays-match.svg)](https://badge.fury.io/js/%40writetome51%2Farrays-match) ![NpmLicense](https://img.shields.io/npm/l/%40writetome51%2Farrays-match.svg) ![npm](https://img.shields.io/npm/dw/%40writetome51%2Farrays-match.svg)

# 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
```js
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
```js
import {arraysMatch} from '@writetome51/arrays-match';
```