Group by element in array by keyword

前端 未结 3 1272
醉酒成梦
醉酒成梦 2021-01-24 22:55

I am developing an application on AngularJS (1) and I can not figure out how to split array of items in another array group by item.

I mean I have an array of different

3条回答
  •  时光说笑
    2021-01-24 23:13

    You could use a hash table and collect the object in the arrays of the hash table.

    var array = [{ name: "toto", uuid: 1111 }, { name: "tata", uuid: 2222 }, { name: "titi", uuid: 1111 }],
        hash = Object.create(null),
        result = [];
    
    array.forEach(function (a) {
        if (!hash[a.uuid]) {
            hash[a.uuid] = [];
            result.push(hash[a.uuid]);
        }
        hash[a.uuid].push(a);
    });
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

提交回复
热议问题