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