How to Check For Array Equality in Javascript

In this article, you will learn how to check for array equality in Javascript. Let’s say you have 3 arrays. Check For Array Equality In…

The post How to Check For Array Equality in Javascript appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli

In this article, you will learn how to check for array equality in Javascript.

Let’s say you have 3 arrays.

var a = [1, 2, 3];
var b = [1, 2, 3];
var c = [5, 6, 7];

Check For Array Equality

In order to check for array equality, you can use the Array.length property and Array.every() method.

var a = [1, 2, 3];
var b = [1, 2, 3];
var c = [5, 6, 7];
  
// Check if every element in firstArray is strictly equals to the corresponding
// element in secondArray
function checkEveryElement(a, b) {
	return a.every((v, i) => v === b[i]);
}

// Check if both arrays have equal length and every element is strictly equal
// in both arrays
function arrayEquality(a, b) {
	if (a.length === b.length && checkEveryElement(a, b)) {
  	console.log("[" + a + "] and [" + b + "] are equal");
  }
  
  else {
  	console.log("[" + a + "] and [" + b + "] are not equal");
  }
}

arrayEquality(a, b);
// => "[1,2,3] and [1,2,3] are equal"

arrayEquality(b, c);
// => "[1,2,3] and [5,6,7] are not equal"

Note: The Array.length property functions by returning the length of the array. The Array.every() method functions by executing a function on every array element.

The post How to Check For Array Equality in Javascript appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli


Print Share Comment Cite Upload Translate Updates
APA

Ariessa Norramli | Sciencx (2021-03-06T13:26:09+00:00) How to Check For Array Equality in Javascript. Retrieved from https://www.scien.cx/2021/03/06/how-to-check-for-array-equality-in-javascript/

MLA
" » How to Check For Array Equality in Javascript." Ariessa Norramli | Sciencx - Saturday March 6, 2021, https://www.scien.cx/2021/03/06/how-to-check-for-array-equality-in-javascript/
HARVARD
Ariessa Norramli | Sciencx Saturday March 6, 2021 » How to Check For Array Equality in Javascript., viewed ,<https://www.scien.cx/2021/03/06/how-to-check-for-array-equality-in-javascript/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Check For Array Equality in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/06/how-to-check-for-array-equality-in-javascript/
CHICAGO
" » How to Check For Array Equality in Javascript." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/03/06/how-to-check-for-array-equality-in-javascript/
IEEE
" » How to Check For Array Equality in Javascript." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/03/06/how-to-check-for-array-equality-in-javascript/. [Accessed: ]
rf:citation
» How to Check For Array Equality in Javascript | Ariessa Norramli | Sciencx | https://www.scien.cx/2021/03/06/how-to-check-for-array-equality-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.