10 examples of 'javascript check if two arrays have same elements' in JavaScript

Every line of 'javascript check if two arrays have same elements' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
1function areArraysEqual(a, b) {
2 if (!a || !b) {
3 return false
4 }
5
6 if (a.length !== b.length) {
7 return false
8 }
9
10 for (var i = 0, length = a.length; i < length; i++) {
11 if (a[i] !== b[i]) {
12 return false
13 }
14 }
15
16 return true
17}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

392function areArraysEqual(a: Column<any>, b: Column<any>) {
393 const xs = a.__array!, ys = b.__array!;
394 for (let i = 0, _i = a.rowCount; i < _i; i++) {
395 if (xs[i] !== ys[i]) return false;
396 }
397 return true;
398}
662function areArraysEqual(array1, array2) {
663 if (!Array.isArray(array1) || !Array.isArray(array2)) {
664 return false;
665 }
666
667 if (array1.length !== array2.length) {
668 return false;
669 }
670
671 for (let i = 0; i < array1.length; ++i) {
672 if (array1[i] !== array2[i]) {
673 return false;
674 }
675 }
676
677 return true;
678}
288function equal_arrays (array1, array2) {
289 for (let i = 0; i < array1.length; i++) {
290 if (array1[i] != array2[i]) return false;
291 }
292 return true;
293}
43export function arraysEqual(array1, array2) {
44 if (!array1 || !array2) {
45 return false;
46 }
47
48 if (array1.length !== array2.length) {
49 return false;
50 }
51
52 for (let index = 0; index < array1.length; index++) {
53 if (array1[index] instanceof Array && array2[index] instanceof Array) {
54 if (!arraysEqual(array1[index], array2[index])) {
55 return false;
56 }
57 } else if (array1[index] !== array2[index]) {
58 return false;
59 }
60 }
61
62 return true;
63}
522arraysEqual(array1, array2) {
523 if (array1.length !== array2.length) return false;
524 for (let i = array1.length; i--; ) {
525 if (array1[i] !== array2[i]) return false;
526 }
527 return true;
528}
149function isEqualArrays(a, b) {
150 var length, i;
151 if (a == null && b == null) {
152 return true;
153 }
154 if (a == null || b == null) {
155 return false;
156 }
157 if (a.length !== b.length) {
158 return false;
159 }
160 for (i = 0, length = a.length; i < length; i++) {
161 if (a[i] !== b[i]) {
162 return false;
163 }
164 }
165 return true;
166}
87function CompareArrays(a, b) {
88
89 if (Array.isArray(a) === false || Array.isArray(b) === false) {
90 return false;
91 }
92
93 if (a.length !== b.length) {
94 return false;
95 }
96
97 for (let n = 0; n < a.length; n++) {
98 if (a[n] !== b[n]) {
99 return false;
100 }
101 }
102
103 return true;
104}
19export function equalArrays<T>(a: T[], b: T[]) {
20 if (a.length !== b.length) {
21 return false;
22 }
23 return a.every((ai, i) => ai === b[i]);
24}
120function arraysAreEqual(a, b, eq) {
121 return a === b || !!a && !!b && a.length === b.length && a.every(function (x, idx) { return eq(x, b[idx]); });
122}

Related snippets