[英]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?我举了两个数组的例子。这样做的最佳方法是什么?我们可以使用 some() 函数吗?
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你可以使用 loadash docs for isequal
_.isEqual(array1, array2) _.isEqual(array1, array2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.