简体   繁体   中英

I have two array of string. I want to compare both the array by lowerCasing each values in the array an return true/false if it matches

I am giving the example of two array.What is the best way to do this?Can we use some() function?

let firstArray = ['WickedWeed'];
let secondArray = ["wickedweed'];


//Output

true;

If there is only one element, then:

firstArray[0].toLowerCase() == secondArray[0].toLowerCase()

If there are multiple elements, then

firstArray.every((e,i)=>(secondArray[i].toLowerCase()==e.toLowerCase()))

If you don't need to match the index, then:

firstArray.map(e=>e.toLowerCase()).every(e=>secondArray.map(e=>e.toLowerCase()).includes(e))

 var firstArray=["abc","deF","zzz"]; var secondArray=["ABC","DEF","zZz"]; console.log(firstArray.map(e=>e.toLowerCase()).every(e=>secondArray.map(e=>e.toLowerCase()).includes(e)));

This can be computationally quite inefficient for long arrays, in which case it would be better to do it in two steps:

 var firstArray=["abc","deF","zzz"]; var secondArray=["ABC","DEF","zZz"]; var secondArrayl = secondArray.map(e=>e.toLowerCase()); console.log(firstArray.map(e=>e.toLowerCase()).every(e=>secondArrayl.includes(e)));

If you don't need to match every element:

 var firstArray=["abc","deF","zzz"]; var secondArray=["ABC"]; var secondArrayl = secondArray.map(e=>e.toLowerCase()); console.log(firstArray.map(e=>e.toLowerCase()).some(e=>secondArrayl.includes(e)));

you can use loadash docs for isequal

_.isEqual(array1, array2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM