Home > OS >  How to check if two arrays are equal or not in typescript? is there any built in function apart from
How to check if two arrays are equal or not in typescript? is there any built in function apart from

Time:09-28

array looks like

var a=['#1 xyz','#2 zbc'];
var b=['#1 xyz','#2 zbc'];

Ideally I want to compare 2 arrays of string and do some action based on that.

CodePudding user response:

If your use case is limited to two arrays of string, you can write custom code rather than use one of the more generic functions pointed out in the comments.

const isEqual = a.length === b.length && a.every((item, i) => b[i] === item);

CodePudding user response:

If its just an arrays of strings then you can use this, do note it only works for scalars

array1.length === array2.length && array1.every((value, index) => value === array2[index])
  • Related