10 examples of 'comparing elements in the array' in JavaScript

Every line of 'comparing elements in the array' 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
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}
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

51function arrayElementsEqu(a?: any[], b?: any[]) {
52 if (!a || !b) {
53 return a === b;
54 }
55
56 const lenA = a.length;
57 const lenB = b.length;
58
59 if (lenA !== lenB) {
60 return false;
61 }
62
63 for (let i = 0; i < lenA; i++) {
64 if (a[i] !== b[i]) {
65 return false;
66 }
67 }
68
69 return true;
70}
52export function compareArray(array1, array2) {
53 if (array2.length !== array1.length) {
54 return false;
55 }
56 for (var i = 0; i < array1.length; i++) {
57 if (array2[i].compareArray) {
58 if (!array2[i].compareArray(array1[i])) {
59 return false;
60 }
61 }
62 if (array2[i] !== array1[i]) {
63 return false;
64 }
65 }
66 return true;
67}
77function compareSortedArrays(array1, array2) {
78 if (array1.length !== array2.length) {
79 return false;
80 }
81 for (let i = 0, len = array1.length; i < len; i++) {
82 if (array1[i] !== array2[i]) {
83 return false;
84 }
85 }
86 return true;
87}
7function compare_arrays(a1, a2) {
8 for(var k in a1) {
9 if(a1[k] !== a2[k]) {
10 return false;
11 }
12 }
13 for(var k in a2) {
14 if(a2[k] !== a1[k]) {
15 return false;
16 }
17 }
18 return true;
19}
77function matchArrayElements(a: string[], b: string[]) {
78 if (a.length !== b.length) return false;
79 for (let i = 0; i < a.length; i++) {
80 if (a[i] !== b[i]) return false;
81 }
82 return true;
83}
524export function arraySortedCmp<T> (array: T[], cmp: (a: T, b: T) => number) {
525 for (let i = 1, il = array.length; i < il; ++i) {
526 if (cmp(array[ i - 1 ], array[ i ]) > 0) return false
527 }
528 return true
529}
96function compareArray(a1, a2){
97 var ret = true;
98 var i = 0;
99 var len = a1.length;
100 if(len != a2.length){
101 ret = false;
102 } else {
103 for(; i < len; i++){
104 if(a1[i] != a2[i]){
105 ret = false;
106 break;
107 }
108 }
109 }
110 return ret;
111}
145function testArray(array: number[], useComparator: boolean) {
146 function comparator(a: number, b: number) {
147 return a < b ? -1 : (a > b ? 1 : 0);
148 }
149
150 for (let value = -2; value <= 12; ++value) {
151 let index = useComparator ? array.upperBound(value, comparator) : array.upperBound(value);
152 assert.isTrue(0 <= index && index <= array.length, 'index is out of bounds');
153 assert.isTrue(index === 0 || array[index - 1] <= value, 'array[index - 1] > value');
154 assert.isTrue(index === array.length || array[index] > value, 'array[index] <= value');
155 }
156}
36static isEqual(arr1, arr2) {
37 if (!(arr1 && arr2)) return false;
38 if (arr1.length !== arr2.length) return false;
39 for (let i = 0, l = arr2.length; i < l; i++) {
40 if (arr1[i] !== arr2[i]) {
41 return false;
42 }
43 }
44 return true;
45}

Related snippets