Does JavaScript support array/list comprehensions like Python?

前端 未结 8 2045
名媛妹妹
名媛妹妹 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:04

    JavaScript no longer supports array comprehensions.

    I too was looking for the JavaScript equivalent. Mozilla Developer's Network indicates that this functionality is no longer supported. The preferred syntax is referenced in the aforementioned link.

    0 讨论(0)
  • 2020-12-01 12:04

    For "completeness"-sake, here's a shorter regexp version.

    var str =  "1234-5";
    var ignore = "-=";
    
    console.log(str.replace(new RegExp(ignore.split("").join("|")), "").split(""));
    

    EDIT: To make sure that RegExp does not "choke" on special characters, ignore can be implemented as regexp literal, instead of a string:

    var str =  "1234-5";
    var ignore = /[\+=-]/;
    console.log(str.replace(ignore, "").split(""));
    
    0 讨论(0)
  • 2020-12-01 12:06

    Not directly, but it's not hard to replicate.

    var string = "1234-5";
    
    var forbidden = "-";
    
    string.split("").filter(function(str){
        if(forbidden.indexOf(str) < 0) {
            return str;
        }
    }).forEach(function(letter) { console.log(letter);});
    

    I guess more directly:

    for(var i=0 ; i < str.length ; i++) {
        if(forbidden.indexOf(str) < 0) {
            console.log(str[i]);
        }
    }
    

    But there's no built in way to filter in your for loop.

    0 讨论(0)
  • 2020-12-01 12:09

    Update: Array comprehensions were removed from the standard. Quoting MDN:

    The array comprehensions syntax is non-standard and removed starting with Firefox 58. For future-facing usages, consider using Array.prototype.map, Array.prototype.filter, arrow functions, and spread syntax.

    See this answer for an example with Array.prototype.map:

    let emails = people.map(({ email }) => email);

    Original answer:

    Yes, JavaScript will support array comprehensions in the upcoming EcmaScript version 7.

    Here's an example.

    var str =  "1234-5";
    var ignore = "-";
    
    console.log([for (i of str) if (!ignore.includes(i)) i]);
    
    0 讨论(0)
  • 2020-12-01 12:09

    You could easily achieve this behavior using an application functor.

    Array.prototype.ap = function(xs) {
      return this.reduce((acc, f) => acc.concat(xs.map(f)), []) 
    }
    
    
    const result = [x => x +1].ap([2])
    console.log(result)
    
    0 讨论(0)
  • 2020-12-01 12:12

    It does have a poor mans version

    const string = '1234-5'
    
    const forbidden = '-'
    
    print([int(i) for i in str(string) if i not in forbidden])
    const result = string.split('').filter(char => char !== forbidden);
    console.log(result)
    

    In JS you can only iterate over single elements in array, so no extraction of multiple entries at a time like in Python.

    For this particular case you should use a RegExp to filter the string though.

    0 讨论(0)
提交回复
热议问题