
- 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
Equality of two 2-D arrays - 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 −
arr1[i][j] === arr2[i][j]
The above should yield true for all i between [0, number of rows] and j between [0, number of columns]
Example
Let’s write the code for this function −
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
- Equality of two arrays JavaScript
- Program to test the equality of two arrays - JavaScript
- Checking for the similarity of two 2-D arrays in JavaScript
- Check two float arrays for equality in Java
- How to compare two arrays for equality in Perl?
- Take in two 2-D arrays of numbers and returns their matrix multiplication result- JavaScript
- Column sum of elements of 2-D arrays in JavaScript
- Intersection of two arrays JavaScript
- Strict equality vs Loose equality in JavaScript.
- isSubset of two arrays in JavaScript
- Reverse sum of two arrays in JavaScript
- Equality of corresponding elements in JavaScript
- Explain equality of objects in JavaScript.
- Alternatively merging two arrays - JavaScript
- Joining two Arrays in Javascript

Advertisements