简体   繁体   English

如何检查数组中的一个元素是否与同一数组中的另一个元素匹配?

[英]How to check if one element of an array matches another element in same array?

Very new to javascript so bear with me... I need to check one element of an array(arr[1]), which contains a string, against another element of the same array(arr[0]) to determine if any letters included in element arr[1] are included in arr[0]. javascript的新手,请耐心等待...我需要针对同一数组(arr [0])的另一个元素检查包含字符串的array(arr [1])的一个元素,以确定是否有字母元素arr [1]中包含的元素包含在arr [0]中。 Those letters can be in any order, upper or lower case, and don't have to occur the same number of times (ie arr[0]="hheyyy" and arr[1]="hey" is fine). 这些字母可以是大写或小写的任何顺序,并且不必出现相同的次数(即arr [0] =“ hheyyy”和arr [1] =“ hey”可以)。 This is what i have (which works) but I was curious if anyone has a better/more simple way of doing this? 这就是我所拥有的(有效的),但是我很好奇是否有人有更好/更简单的方法? -thanks in advance. -提前致谢。

   function mutation(arr) {
     //splits the array into two separate arrays of individual letters
      var newArr0 = arr.join('').toLowerCase().split('').slice(0,arr[0].length);
      var newArr1 = arr.join('').toLowerCase().split('').slice(arr[0].length);
      var boolArr = [];
     //checks each letter of arr1 to see if it is included in any letter of arr0
      for(var i = 0; i < newArr1.length; i++)
        boolArr.push(newArr0.includes(newArr1[i])); 
     //results are pushed into an array of boolean values
      if (boolArr.indexOf(false) !==-1) 
        return false; //if any of those values are false return false
      else return true;
    }

    mutation(["hello", "hey"]); //returns false

You could use a regular expression: 您可以使用正则表达式:

function mutationReg(arr) {
  return !arr[1].replace(new RegExp('['+arr[0].replace(/(.)/g,'\\\\$1')+']', "gi"), '').length;
}

This escapes every character in the second string with backslash (so it cannot conflict with regular expression syntax), surrounds it with square brackets, and uses that as a search pattern on the first string. 这会用反斜杠转义第二个字符串中的每个字符(因此它不会与正则表达式语法冲突),并用方括号将其括起来,并将其用作第一个字符串上的搜索模式。 Any matches (case-insensitive) are removed from the result, so that only characters are left over that don't occur in the second string. 从结果中删除所有匹配项(不区分大小写),以便仅保留不出现在第二个字符串中的字符。 The length of the result is thus an indication on whether there was success or not. 因此,结果的长度表明是否成功。 Applying the ! 应用! to it gives the correct boolean result. 给出正确的布尔结果。

This might not be the fastest solution. 这可能不是最快的解决方案。

Here is another ES6 alternative using a Set for good performance: 这是另一种使用Set ES6替代方案,以实现良好的性能:

function mutation(arr) {
  var chars = new Set([...arr[0].toLowerCase()]);
  return [...arr[1].toLowerCase()].every (c => chars.has(c));
}

You can use Array.from() to convert string to an array, Array.prototype.every() , String.prototype.indexOf() to check if every charactcer in string converted to array is contained in string of other array element. 您可以使用Array.from()将字符串转换为数组,使用Array.prototype.every()String.prototype.indexOf()来检查转换为数组的字符串中的每个字符是否包含在其他数组元素的字符串中。

 var arr = ["abc", "cab"]; var bool = Array.from(arr[0]).every(el => arr[1].indexOf(el) > -1); console.log(bool); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查另一个数组中一个数组的元素 - check element of one array in another array 如何检测数组中的元素是一个字符长,并且匹配另一个数组中任何元素的第一个字符 - How to detect is an element in an array is one character long and matches the first character of any element in another array Coffeescript:Array元素与另一个数组匹配 - Coffeescript: Array element matches another array 如何将数组作为一个元素推入另一个数组 - How to push array into another array as one element 如何修改数组数组,取决于其中的元素是否与另一个数组数组中的元素匹配? - How to modify an array of array, depending if an element in it matches an element from another array of array? 检查是否正在使用数组中的一个元素,如果使用,则使用另一个元素 - Check if one element in array is being used, if so, use another element 检查数组的某个元素是否与以下相同 - Check if an element of the array is the same as the following 将元素插入同一数组中的另一个元素内 - Insert element inside another element in the same array 如何检查数组中的元素是否在另一个数组中? (嵌套循环) - How to check if an element in an array is in another array? (nested for loops) 检查另一个数组中数组中的元素是否存在 - Check element presence in array in another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM