简体   繁体   English

我有两个字符串数组。 我想通过lowerCasing数组中的每个值来比较两个数组,如果匹配则返回true/false

[英]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.

相关问题 我有两个数组。 我想以一种可以从另一个数组中获取最接近的值的方式比较它们 - I have two array. I want to compare both of them in a way that I can get the nearest value from the other array 我有两个 arrays 里面的对象我想比较值并返回更改键值 pait 和数组 position - I have two arrays with objects inside that I want to compare the values and return the change key value pait and array position 为每个数组返回 true 或 false 以与一个过滤器数组进行比较的问题 - Problem on returning true or false for each array to compare with one filter array 我有一个JavaScript数组,我想下载数组中的每个图像 - I have a JavaScript array and I want to download each image in the array 比较两个对象数组,如果匹配则返回 true - Compare two array of objects and return true if they are match 如果数组的所有值都为真,如何返回真,否则返回假? - How to return true if all values of array are true otherwise return false? javascript:通过某个 id 比较两个对象数组的内容并返回 true 或 false - javascript : compare content of two array of objects by some id and return true or false 将true / false数组与其他数组进行比较 - Compare true/false array with other array 我有一个包含 2 个 object 的数组,在每个 object 中我有一个 object 数组。 我只想获取与属性匹配的那些数据 - I have an array which has 2 object, In each object i have a array of object. I want to get only those data's which matches the attributes 在对象数组中查找完全匹配时是否要返回true或false? - Want to return true or false when finding an exact match in an array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM