简体   繁体   English

将Python代码实现为Javascript时,我在做什么错?

[英]What i'm doing wrong when implementing Python code to Javascript?

I have the next python code: 我有下一个python代码:

letter = ['a', 'b', 'c', 'd']
word = "Black"
for i in word:
    if i in letter:
        print(i)

So, I'm trying to do the same in JS : 所以,我试图在JS中做同样的事情:

var letter = ['a', 'b', 'c', 'd'];
var word = "Black";
var dictionary = [];
var dictionaryCoincidence = [];

for (var i = 0; i < word.length; i++) {
    dictionary.push(word[i]);
}

for (var i = 0; i < dictionary.length; i++) {
    if (dictionary[i] == letter[i]) {
        dictionaryCoincidence.push(dictionary[i]);
    }
}

console.log(dictionary);
console.log(dictionaryCoincidence);

But, it doesn't work! 但是,它不起作用!

You can use Array.includes() to check if some element belongs to an array, then use a for ... of loop to traverse the string and do something like this: 您可以使用Array.includes()检查某个元素是否属于数组,然后使用for ... of循环遍历字符串并执行以下操作:

 const letter = ['a', 'b', 'c', 'd']; const word = "Black"; for (const char of word) { if (letter.includes(char)) console.log(char); } 
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;} 

In the spirit of functional python I suggest using filter since it works like the filter in python. 本着功能性python的精神,我建议使用filter因为它的工作方式类似于python中的filter The question is on both Python and Javascript so :) 问题在Python和Javascript上都存在,所以:)

 var letter = ['a', 'b', 'c', 'd']; var word = "Black"; var filtered = word.split("").filter(x => letter.includes(x)) console.log(filtered) 

You don't really need second for loop.. Just check the word[i] is in the letter array or not, and you can do it by using "includes" or "indexOf".. 您实际上并不需要第二个for循环。只需检查单词[i]是否在字母数组中,就可以使用“ includes”或“ indexOf”来实现。

 var letter = ['a', 'b', 'c', 'd']; var word = "Black"; var dictionary = []; var dictionaryCoincidence = []; for (var i = 0; i < word.length; i++) { dictionary.push(word[i]); if (letter.includes(word[i])) { dictionaryCoincidence.push(dictionary[i]); } } console.log(dictionary); console.log(dictionaryCoincidence); 

You must use two loop : one loop for dictionary and one loop for letters like this: 您必须使用两个循环:一个循环用于字典,一个循环用于这样的字母:

    var letter = ['a', 'b', 'c', 'd'];
    var word = "Blackd";
    var dictionary = [];
    var dictionaryCoincidence = [];

   for (var i = 0; i < word.length; i++) {
    dictionary.push(word[i]);
   }

   for (var i = 0; i < dictionary.length; i++) {
       for(var j = 0; j < letter.length ; j++){
           if (dictionary[i] == letter[j]) {
             dictionaryCoincidence.push(dictionary[i]);
           }
        }
   }

   console.log(dictionary);
   console.log(dictionaryCoincidence);

Try changing the second loop to 尝试将第二个循环更改为

for (var i = 0; i < dictionary.length; i++) {
    if (letter.includes(dictionary[i])) {
        dictionaryCoincidence.push(dictionary[i]);
    }
}

You need to check that the dictionary[i] is in the letter array, and not that it equals to letter[i] 您需要检查字典[i]是否在字母数组中,而不是等于字母[i]

  1. It gives you the result [a,c] . 它给你结果[a,c]。 B is not included, because it's uppercase. 不包括B,因为它是大写的。 Cast dictionary[i] to lowercase if you need the letter B to. 如果需要字母B,则将dictionary [i]强制转换为小写。
  2. You can use letter.indexOf method instead of includes(). 您可以使用letter.indexOf方法而不是include()。 So condition letter.indexOf(dictionary[i])>-1 also works 所以条件letter.indexOf(dictionary [i])>-1也可以

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

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