Does JavaScript support array/list comprehensions like Python?

前端 未结 8 2117
名媛妹妹
名媛妹妹 2020-12-01 11:31

I\'m practicing/studying both JavaScript and Python. I\'m wondering if Javascript has the equivalence to this type of coding.

I\'m basically trying to get an array

8条回答
  •  离开以前
    2020-12-01 12:13

    Reading the code, I assume forbidden can have more than 1 character. I'm also assuming the output should be "12345"

    var string = "12=34-5";
    
    var forbidden = "=-";
    
    console.log(string.split("").filter(function(str){
        return forbidden.indexOf(str) < 0;
    }).join(""))
    

    If the output is "1" "2" "3" "4" "5" on separate lines

    var string = "12=34-5";
    
    var forbidden = "=-";
    
    string.split("").forEach(function(str){
        if (forbidden.indexOf(str) < 0) {
            console.log(str);
        }
    });
    

提交回复
热议问题