ionic数组元素分组

2016-11-02  本文已影响92人  Lem0nNat1on
$scope.click = function() {
        // body...
        var arr = [{id:1,name:"aaa"},{id:1,name:"bbb"},{id:2,name:"ccc"},{id:1,name:"ddd"},{id:2,name:"eee"},{id:3,name:"fff"}]
        var group = {};
        for(var i in arr){
            if(group[arr[i].id]){
                group[arr[i].id].push(arr[i]);
            }else{
                group[arr[i].id] = [arr[i]];
            }
        }
        console.log(group)
    }   

结果:

QQ截图20161102105434.png
上一篇下一篇

猜你喜欢

热点阅读