
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Checking for the similarity of two 2-D arrays in JavaScript
We are required to write a JavaScript function that takes in two 2-D arrays and returns a boolean based on the check whether the arrays are equal or not. The equality of these arrays, in our case, is determined by the equality of corresponding elements.
Both the arrays should have same number of rows and columns. Also, arr1[i][j] === arr2[i][j] should yield true for all i between [0, number of rows] and j between [0, number of columns]
Example
The code for this will be −
const arr1 = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], ]; const arr2 = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], ]; const areEqual = (first, second) => { const { length: l1 } = first; const { length: l2 } = second; if(l1 !== l2){ return false; }; for(let i = 0; i < l1; i++){ for(j = 0; j < first[i].length; j++){ if(first[i][j] !== second[i][j]){ return false; }; }; }; return true; }; console.log(areEqual(arr1, arr2));
Output
The output in the console −
true
- Related Articles
- Checking for ascending arrays in JavaScript
- Equality of two 2-D arrays - JavaScript
- Checking for special type of Arrays in JavaScript
- Checking for squared similarly of arrays in JavaScript
- Checking for centrally peaked arrays in JavaScript
- Checking if two arrays can form a sequence - JavaScript
- Column sum of elements of 2-D arrays in JavaScript
- Take in two 2-D arrays of numbers and returns their matrix multiplication result- JavaScript
- Checking for the Gapful numbers in JavaScript
- isSubset of two arrays in JavaScript
- Search for documents with similar arrays in MongoDB and order by similarity value
- Checking for uniqueness of a string in JavaScript
- Checking for permutation of a palindrome in JavaScript
- Checking for particular types of matrix in JavaScript
- Finding the continuity of two arrays in JavaScript

Advertisements