Javascript Array groupBy(key=null)

Description

Javascript Array groupBy(key=null)


const equals = (v,w) => v===w/*from   w w w.  j  a  v a 2 s .  co  m*/

Array.prototype.groupBy = function groupBy(key=null) {
  const condition = (key||equals)
  var res =[]
  var temp=[this[0]]
  
  if (this.length==0) return []
  
  for (var e of this.slice(1)){
    if (temp.length==0 || condition(e, temp.slice(-1)[0])) temp.push(e)
    else {
      res.push(temp)
      temp=[]
      temp.push(e)
    }
  }
  if (temp.length!=0) res.push(temp)
  return res
};

Object.defineProperty(Array.prototype,"groupBy",{enumerable:false});



PreviousNext

Related