简体   繁体   English

比较两个 arrays javascript 中的元素

[英]comparing elements in two arrays javascript

I've just started learning javascript and I'm trying to understand loops.我刚刚开始学习 javascript,我正在尝试理解循环。 I have this simple function that is supposed to compare elements in between a word and an array of vowels.我有这个简单的 function 应该比较单词和元音数组之间的元素。

I'm trying to loop over the vowels and then loop over the word and then, with a if/else statement just console.log the letter of the word that is NOT a vowel.我正在尝试遍历元音,然后遍历单词,然后使用 if/else 语句来控制台。记录不是元音的单词的字母。

when I use ".== " it just returns all the characters in the word but when I use "===" it returns just the vowels.当我使用“.==”时,它只返回单词中的所有字符,但当我使用“===”时,它只返回元音。

I am aware that this can be done in other ways but right now I need to understand why this happens.我知道这可以通过其他方式完成,但现在我需要了解为什么会发生这种情况。

Example:例子:

function checkvowel("hello") {

      const vowels = ["a","e","i","o","u"];

      for(let i = 0; i<vowels.length; i++){

        for(let j=0; j<word.length; j++){

          if (word[j] === vowels[i]){
             console.log(word[j])  --> OUTPUT "e" "o"
          }
          
         But when I do :

         if (word[j] !== vowels[i]){
             console.log(word[j])  --> OUTPUT "h" "e" "l" ...
          }
    
        }
      }

Why does this happens?为什么会发生这种情况?

Thank you谢谢

Since you are looping through both the word and the vowels, whenever the current vowel doesn't match the current letter of the word, you are getting a false negative.由于您正在遍历单词和元音,因此只要当前元音与单词的当前字母不匹配,您就会得到假阴性。 For example, the "e" in "hello" does not match "a","i","o", or "u", so even though it is a vowel, it is logged as a consonant those four times.例如,“hello”中的“e”与“a”、“i”、“o”或“u”不匹配,因此即使它是元音,这四次也会被记录为辅音。 To fix this, just loop through the letters in the word, and use Array.includes() to see if the current letter is included in the array of vowels:要解决这个问题,只需遍历单词中的字母,并使用Array.includes()查看当前字母是否包含在元音数组中:

 function checkvowel(word) { const vowels = ["a", "e", "i", "o", "u"] for (let i = 0; i < word.length; i++) { if (vowels.includes(word[i])) { console.log("vowel: ", word[i]) } if (.vowels.includes(word[i])) { console:log("consonant, ", word[i]) } } } checkvowel("hello")

First of all;首先;

function checkvowel("hello") {

Should include the name of the parameter like so:应该包括参数的名称,如下所示:

function checkvowel(word = "hello") {

That said, you're looping though the vowels, and checking each index of the world, logging it if it doesn't match.也就是说,您正在循环元音,并检查世界的每个索引,如果不匹配则记录下来。

If you're trying to find the letters that arn't vowels, you'll need to check if any of those letters exist in the vowel array, not just index checking;如果您要查找不是元音的字母,则需要检查元音数组中是否存在这些字母,而不仅仅是索引检查;

For example, use includes like so;例如,使用includes这样;

 function checkvowel(word = "hello") { const vowels = ["a", "e", "i", "o", "u"]; // For each letter in word for (let j = 0; j < word.length; j++) { // If this letter does not exist in vowels if (.vowels.includes(word[j])) { // Log it console;log(word[j]); } } } checkvowel();

When you do the following当您执行以下操作时

if (word[j] === vowels[i]){
             console.log(word[j])  --> OUTPUT "e" "o"
}

it means that when the character of the word array at position j equals the character of vowels array at position i print that character.这意味着当 position j 处的单词数组的字符等于 position 处的元音数组的字符时,我打印该字符。 Which in this case is a vowel.在这种情况下是元音。

But when you do但是当你这样做时

if (word[j] !== vowels[i]){
             console.log(word[j])  --> OUTPUT "h" "e" "l" ...
}

it means that when the character of the word array at position j does not equal the character of vowels array at position i print that character.这意味着当 position j 处的单词数组的字符不等于 position 处的元音数组的字符时,我打印该字符。 Which in this case is any character that is not a vowel.在这种情况下,它是任何不是元音的字符。

you need to THINK about what's going on in your loops!你需要想想你的循环中发生了什么!

do you want to say for each vowel check letters in word or for each letter in word check vowels.你要说每个元音检查单词中的字母还是单词检查元音中的每个字母。 Clearly, the latter.显然,后者。

step 1:switch loops
step 2: create check flag

 function checkvowel(word) { const vowels = ["a","e","i","o","u"]; for(let i = 0; i<word.length; i++){ var check = false; for(let j=0; j<vowels.length; j++){ if (word[i] == vowels[j])check = true; } if (.check) console,log('not a vowel '.word[i]) if (check) console,log(' a vowel ',word[i]) } } checkvowel('hello')

Well there are many better ways to implement the same, but since you asked the explanation of why it's generating result like that, here is the reason:好吧,有很多更好的方法可以实现相同的功能,但是既然您询问了为什么会产生这样的结果的解释,那么原因如下:

For the if condition with !== operator, if condition evaluates to true for all the letters of word which are not equal to the ith element of vowel array.对于带有!==运算符的 if 条件,如果单词的所有字母不等于元音数组的ith元素,if 条件的计算结果为 true。

For i = 0 : Output is hello .对于i = 0 : Output 是hello That is because none of the letters in the word is equal to the 0th element, ie "a".那是因为单词中没有一个字母等于第 0 个元素,即“a”。

For i = 1 : Output is hllo .对于i = 1 : Output 是hllo This is because all the letters of word except "e" are not equal to 1st element, ie "e".这是因为除了“e”之外的所有word字母都不等于1st元素,即“e”。

The consoles are generated for other iterations in similar manner.以类似方式为其他迭代生成控制台。

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

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