[Vuejs]-Check 2 arrays if the result is equal in vue

0๐Ÿ‘

โœ…

You can create a function which checks if they are equal by doing the following:

let A = ['a', 'b', 'c']
let B = ['a', 'b', 'c']
let C = ['a', 'b', 'd']
    
function isEqual(arr1, arr2) {
  for (let i = 0; i < arr2.length; i++) {
    if (arr1[i] !== arr2[i]) return false;
  }
  // return true if above checks are passed
  return true;
}
    
console.log(isEqual(A, B))
console.log(isEqual(A, C))

EDIT:
You should add a if check to avoid nullpointerexceptions

function isEqual(arr1, arr2) {
   if (arr1.length !== arr2.length) return false;
}

0๐Ÿ‘

An easier way would be using JSON.stringify
(ie)

if(JSON.stringify(arr1) === JSON.stringify(arr2))

Using the above you can compare multidimensional array of n level and no for-loops are needed

Leave a comment