Does JavaScript support array/list comprehensions like Python?

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

提交回复
热议问题