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

js 数组分组

程序员文章站 2022-06-17 19:46:40
...
  //根据组合操作
    this.group = function () {
        var arg = getArg(arguments);
        var o = arg["A"][0] || [];
        var field = arg["s"][0] || arg["A"][1] || null;
        var judge = arg["f"][0] || function () {};
        if (o.constructor == Array) {
            var tmp = {};
            var ret = [];
            if (field) {
                var fields = (field.constructor == Array) ? field : field.split(',');
                var getItem = function (item) {
                    var ret = {};
                    for (var j = 0, jLen = fields.length; j < jLen; j++) {
                        var key = fields[j];
                        ret[key] = item[key];
                    }
                    return ret;
                }
                var getKey = function (item) {
                    var ret = "";
                    for (var j = 0, jLen = fields.length; j < jLen; j++) {
                        var key = fields[j];
                        var itemKey = item[key] == null ? "" : item[key];
                        ret += "_" + itemKey;
                    }
                    return ret;
                }
                for (var i = 0, len = o.length; i < len; i++) {
                    var item = o[i];
                    var key = getKey(item);
                    if (tmp[key]) {
                        tmp[key].data.push(item);
                    } else {
                        tmp[key] = {
                            item: getItem(item),
                            data: [item]
                        };
                    }
                }
                for (var key in tmp) {
                    judge(tmp[key].item, tmp[key].data);
                    ret.push(tmp[key].item);
                }
                return ret;
            }
        }
    }
   var getArg = function (arg) {
        var ret = {
            'n': [], //number
            's': [], //string
            'b': [], //boolean
            'o': [], //object
            'O': [], //Object
            'A': [], //Array
            'f': [], //function
            'D': [], //Date
            'nl': [], //null
            'v': [] //value就是值
        }
        for (var i = 0, len = arg.length; i < len; i++) {
            var ar = arg[i];
            if (typeof (ar) == 'number') {
                ret['n'].push(ar);
                ret['v'].push(ar);
            } else if (typeof (ar) == 'string') {
                ret['s'].push(ar);
                ret['v'].push(ar);
            } else if (typeof (ar) == 'boolean') {
                ret['b'].push(ar);
                ret['v'].push(ar);
            } else if (typeof (ar) == 'function') {
                ret['f'].push(ar);
            } else if (typeof (ar) == 'object') {
                if (ar) {
                    if (ar.constructor == Object) {
                        ret['o'].push(ar);
                        ret['O'].push(ar);
                    } else if (ar.constructor == Array) {
                        ret['o'].push(ar);
                        ret['A'].push(ar);
                    } else if (ar.constructor == Date) {
                        ret['D'].push(ar);
                    }
                } else {
                    ret['nl'].push(ar);
                    ret['v'].push(ar);
                }
            }
        }
        return ret;
    }
   
   

想用更多的方法请打开JavaScript基础包

js 数组分组

转载于:https://my.oschina.net/u/3875237/blog/3053358