如何以javascript或角度方式返回此结果对象格式列表?

如何以javascript或角度方式返回此结果对象格式列表?,javascript,angularjs,Javascript,Angularjs,我对javascript中的for循环或使用angular方式输出结果对象列表有疑问 我有一个对象列表如下 var alist = []; alist = [ { 'code': 1000, 'type': 'C', 'a': 4, 'b': null, 'c': null }, { 'code': 1000, 'type': 'C', 'a': null, 'b': null, 'c': 6 }, { 'code': 1500, 'typ

我对javascript中的for循环或使用angular方式输出结果对象列表有疑问

我有一个对象列表如下

var alist = [];
    alist = [
        { 'code': 1000, 'type': 'C', 'a': 4, 'b': null, 'c': null },
        { 'code': 1000, 'type': 'C', 'a': null, 'b': null, 'c': 6 },
        { 'code': 1500, 'type': 'O', 'a': null, 'b': null, 'c': 8 },
        { 'code': 1500, 'type': 'O', 'a': null, 'b': 8, 'c': null },
        { 'code': 1000, 'type': 'O', 'a': 7, 'b': null, 'c': null },
        { 'code': 1000, 'type': 'C', 'a': null, 'b': 7, 'c': null },
        { 'code': 2000, 'type': 'C', 'a': 4, 'b': null, 'c': null },
        { 'code': 2000, 'type': 'C', 'a': null, 'b': 6, 'c': 12 },
        { 'code': 3000, 'type': 'C', 'a': 1, 'b': null, 'c': 12 },
        { 'code': 3000, 'type': 'C', 'a': null, 'b': 6, 'c': null }
    ];
我想要的结果是结合相同的代码+相同的类型,并结合a,b,c值。 这是我想要的结果

var resultList = [];
    resultList = [
        { 'code': 1000, 'type': 'C', 'a': 4, 'b': 7, 'c': 6 },
        { 'code': 1500, 'type': 'O', 'a': null, 'b': 8, 'c': 8 },
        { 'code': 1000, 'type': 'O', 'a': 7, 'b': null, 'c': null },
        { 'code': 2000, 'type': 'C', 'a': 4, 'b': 6, 'c': 12 },
        { 'code': 3000, 'type': 'C', 'a': 1, 'b': 6, 'c': 12 },
    ];
关键点:

  • 代码是动态的,而不是1000、1500、2000的硬代码
  • 一个代码只绑定一个类型,所以如果1000个代码只包含一个C类型
  • 是否可以输出此结果


    我尝试了,但仍然没有输出结果列表。

    如果您想在纯javascript中更改数据,可以像这样使用
    reduce()

    var-alist=[
    {'code':1000,'type':'C','a':4,'b':null,'C':null},
    {'code':1000,'type':'C','a':null,'b':null,'C':6},
    {'code':1500,'type':'O','a':null,'b':null,'c':8},
    {'code':1500,'type':'O','a':null,'b':8,'c':null},
    {'code':1000,'type':'O','a':7,'b':null,'c':null},
    {'code':1000,'type':'C','a':null,'b':7,'C':null},
    {'code':2000,'type':'C','a':4,'b':null,'C':null},
    {'code':2000,'type':'C','a':null,'b':6,'C':12},
    {'code':3000,'type':'C','a':1,'b':null,'C':12},
    {'code':3000,'type':'C','a':null,'b':6,'C':null}
    ];
    var obj={}
    var结果=列表减少(函数(r,e){
    var key=e.code+“|”+e.type;
    如果(!obj[key]){
    obj[key]=e;
    r、 推(obj[键]);
    }否则{
    Object.key(obj[key]).forEach(函数(el){
    如果(obj[key][el]==null)obj[key][el]=e[el];
    })
    }
    返回r;
    }, [])
    
    log(result)
    如果您想在纯javascript中更改数据,可以像这样使用
    reduce()

    var-alist=[
    {'code':1000,'type':'C','a':4,'b':null,'C':null},
    {'code':1000,'type':'C','a':null,'b':null,'C':6},
    {'code':1500,'type':'O','a':null,'b':null,'c':8},
    {'code':1500,'type':'O','a':null,'b':8,'c':null},
    {'code':1000,'type':'O','a':7,'b':null,'c':null},
    {'code':1000,'type':'C','a':null,'b':7,'C':null},
    {'code':2000,'type':'C','a':4,'b':null,'C':null},
    {'code':2000,'type':'C','a':null,'b':6,'C':12},
    {'code':3000,'type':'C','a':1,'b':null,'C':12},
    {'code':3000,'type':'C','a':null,'b':6,'C':null}
    ];
    var obj={}
    var结果=列表减少(函数(r,e){
    var key=e.code+“|”+e.type;
    如果(!obj[key]){
    obj[key]=e;
    r、 推(obj[键]);
    }否则{
    Object.key(obj[key]).forEach(函数(el){
    如果(obj[key][el]==null)obj[key][el]=e[el];
    })
    }
    返回r;
    }, [])
    
    console.log(result)
    您可以使用一些帮助器数组来收集键和参数,并使用一个对象进行分组

    var data=[{code:1000,类型:'C',a:4,b:null,C:null},{code:1000,类型:'C',a:null,b:null,C:6},{code:1500,类型:'O',a:null,b:null,b:null,C:8},{code:1500,类型:'O',a:null,b:8,C:null},{code:1000,类型:'C',a:null,b:null},{,{code:2000,'type':'C',a:4,b:null,C:null},{code:2000,type'C',a:null,b:6,C:12},{code:3000,type'C',a:1,b:null,C:12},{'code:3000,type'C',a:null,b:6,C:null},
    键=['code','type'],
    值=['a','b','c'],
    分组=[];
    data.forEach(函数(a){
    var key=keys.map(函数(k){returna[k];}).join('|');
    如果(!此[键]){
    此[键]={};
    forEach(函数(k){this[k]=a[k];},this[key]);
    forEach(函数(k){this[k]=null;},this[key]);
    分组。按(此[键]);
    }
    值。forEach(函数(k){
    如果(a[k]!==null){
    这个[k]=(这个[k]| 0)+a[k];
    }
    },此[键];
    },Object.create(null));
    console.log(分组);

    。作为控制台包装{max height:100%!important;top:0;}
    您可以使用一些帮助器数组来收集键和参数,并使用一个对象进行分组

    var data=[{code:1000,类型:'C',a:4,b:null,C:null},{code:1000,类型:'C',a:null,b:null,C:6},{code:1500,类型:'O',a:null,b:null,b:null,C:8},{code:1500,类型:'O',a:null,b:8,C:null},{code:1000,类型:'C',a:null,b:null},{,{code:2000,'type':'C',a:4,b:null,C:null},{code:2000,type'C',a:null,b:6,C:12},{code:3000,type'C',a:1,b:null,C:12},{'code:3000,type'C',a:null,b:6,C:null},
    键=['code','type'],
    值=['a','b','c'],
    分组=[];
    data.forEach(函数(a){
    var key=keys.map(函数(k){returna[k];}).join('|');
    如果(!此[键]){
    此[键]={};
    forEach(函数(k){this[k]=a[k];},this[key]);
    forEach(函数(k){this[k]=null;},this[key]);
    分组。按(此[键]);
    }
    值。forEach(函数(k){
    如果(a[k]!==null){
    这个[k]=(这个[k]| 0)+a[k];
    }
    },此[键];
    },Object.create(null));
    console.log(分组);
    .as控制台包装{max height:100%!important;top:0;}
    使用它可以解决如下问题:

    (function (alist) {
        function mergeObjs(a, b) {
            return _.mergeWith(a, b, function (a, b, key) {
                if (['a', 'b', 'c'].indexOf(key) !== -1) {
                    return _.isNull(a) && _.isNull(b) ? null : (a || 0) + (b || 0);
                }
            });
        }
    
        var res = _.reduce(_.groupBy(alist, 'code'), function (memo, groupedByCode) {
            return _.concat(memo, _.map(_.groupBy(groupedByCode, 'type'), function (groupedByType) {
                return _.reduce(_.tail(groupedByType), mergeObjs, _.head(groupedByType));
            }));
        }, []);
    
        console.log(res);
    })([
        {'code': 1000, 'type': 'C', 'a': 4, 'b': null, 'c': null},
        {'code': 1000, 'type': 'C', 'a': null, 'b': null, 'c': 6},
        {'code': 1500, 'type': 'O', 'a': null, 'b': null, 'c': 8},
        {'code': 1500, 'type': 'O', 'a': null, 'b': 8, 'c': null},
        {'code': 1000, 'type': 'O', 'a': 7, 'b': null, 'c': null},
        {'code': 1000, 'type': 'C', 'a': null, 'b': 7, 'c': null},
        {'code': 2000, 'type': 'C', 'a': 4, 'b': null, 'c': null},
        {'code': 2000, 'type': 'C', 'a': null, 'b': 6, 'c': 12},
        {'code': 3000, 'type': 'C', 'a': 1, 'b': null, 'c': 12},
        {'code': 3000, 'type': 'C', 'a': null, 'b': 6, 'c': null}
    ]);
    
  • 按代码分组
  • 对于每个代码,按类型分组
  • 对于每种类型,根据必要条件合并调整关键点值的对象
  • 虽然它没有其他实现那么快,但它更具声明性,并为您提供了正在发生的事情的提示。我强烈建议使用Lodash。以下是。希望它能有所帮助。

    使用它可以解决以下问题:

    (function (alist) {
        function mergeObjs(a, b) {
            return _.mergeWith(a, b, function (a, b, key) {
                if (['a', 'b', 'c'].indexOf(key) !== -1) {
                    return _.isNull(a) && _.isNull(b) ? null : (a || 0) + (b || 0);
                }
            });
        }
    
        var res = _.reduce(_.groupBy(alist, 'code'), function (memo, groupedByCode) {
            return _.concat(memo, _.map(_.groupBy(groupedByCode, 'type'), function (groupedByType) {
                return _.reduce(_.tail(groupedByType), mergeObjs, _.head(groupedByType));
            }));
        }, []);
    
        console.log(res);
    })([
        {'code': 1000, 'type': 'C', 'a': 4, 'b': null, 'c': null},
        {'code': 1000, 'type': 'C', 'a': null, 'b': null, 'c': 6},
        {'code': 1500, 'type': 'O', 'a': null, 'b': null, 'c': 8},
        {'code': 1500, 'type': 'O', 'a': null, 'b': 8, 'c': null},
        {'code': 1000, 'type': 'O', 'a': 7, 'b': null, 'c': null},
        {'code': 1000, 'type': 'C', 'a': null, 'b': 7, 'c': null},
        {'code': 2000, 'type': 'C', 'a': 4, 'b': null, 'c': null},
        {'code': 2000, 'type': 'C', 'a': null, 'b': 6, 'c': 12},
        {'code': 3000, 'type': 'C', 'a': 1, 'b': null, 'c': 12},
        {'code': 3000, 'type': 'C', 'a': null, 'b': 6, 'c': null}
    ]);
    
  • 按代码分组
  • 对于每个代码,按类型分组
  • 对于每种类型,根据必要条件合并调整关键点值的对象
  • 虽然它不是那么快