欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ES6数据分组

程序员文章站 2022-06-27 20:44:52
ItemGroupBy(arr, key) { let newArr = [], types = {}, newItem, i, j, cur; for (i = 0, j = arr.length; i < j; i++) { cur = arr[i]; if (!(cur[key] in typ ......
  itemgroupby(arr, key) {
    let newarr = [],
        types = {},
        newitem, i, j, cur;
    for (i = 0, j = arr.length; i < j; i++) {
        cur = arr[i];
        if (!(cur[key] in types)) {
            types[cur[key]] = { type: cur[key], data: [] };
            newarr.push(types[cur[key]]);
        }
        types[cur[key]].data.push(cur);
    }
    return newarr;
}