JS GroupBy

function groupBy(array, f,hasKey) {
  debugger;
  const groups = {};
  array.forEach(function (o) {
    const group = hasKey?f(o):JSON.stringify(f(o));
    groups[group] = groups[group] || [];
    groups[group].push(o);
  });
  if(hasKey)return groups;
  return Object.keys(groups).map(function (group) {
    return groups[group];
  });
}
c=groupBy(PRODUCTS,(item)=>(item[‘category‘]),true)

 

JS GroupBy

上一篇:编码gbk ajax的提交


下一篇:scrollUtils.js #触底触发上拉加载更多 #越线 #上下滚动事件