How to do equivalent of LINQ SelectMany() just in javascript

后端 未结 9 1474
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 16:54

Unfortunately, I don\'t have JQuery or Underscore, just pure javascript (IE9 compatible).

I\'m wanting the equivalent of SelectMany() from LINQ functionality.

<
9条回答
  •  臣服心动
    2020-12-13 17:23

    try this (with es6):

     Array.prototype.SelectMany = function (keyGetter) {
     return this.map(x=>keyGetter(x)).reduce((a, b) => a.concat(b)); 
     }
    

    example array :

     var juices=[
     {key:"apple",data:[1,2,3]},
     {key:"banana",data:[4,5,6]},
     {key:"orange",data:[7,8,9]}
     ]
    

    using :

    juices.SelectMany(x=>x.data)
    

提交回复
热议问题