zoukankan      html  css  js  c++  java
  • 数组内多个对象合并相同的属性,值用数组合并

     var strArray = [
                {
                  key: 1, name: 'b01', typeName: '水平', data: '10.0'
                }, {
                  key: 2, name: 'b01', typeName: '竖直', data: '10.50'
                }, {
                  key: 3, name: 'b01', typeName: '斜角', data: '8.2'
                },
                {
                  key: 4, name: 'b02', typeName: '水平', data: '10.0'
                }, {
                  key: 5, name: 'b03', typeName: '水平', data: '10.50'
                }, {
                  key: 6, name: 'b02', typeName: '竖直', data: '8.2'
                }
              ]
              var map = {},
                dest = [];
              for (var i = 0; i < strArray.length; i++) {
                var ai = strArray[i];
                if (!map[ai.name]) { //依赖分组字段可自行更改!
                  dest.push({
                    name: ai.name, //依赖分组字段可自行更改!
                    data: [ai.data],
                    typeName: [ai.typeName]
                  });
                  map[ai.name] = ai; //依赖分组字段可自行更改!
                } else {
                  for (var j = 0; j < dest.length; j++) {
                    var dj = dest[j];
                    if (dj.name == ai.name) { //依赖分组字段可自行更改!
                      dj.data.push(ai.data);
                      dj.typeName.push(ai.typeName);
                      break;
                    }
                  }
                }
              }

              console.log(dest);

              dest.forEach(_val => {
                console.log(_val)
                console.log(_val.name)
                console.log(_val.data)
                _val[_val.name] = _val.data

              })
              console.log(dest);

  • 相关阅读:
    MySQL——视图/触发器/事务/存储过程/函数/流程控制
    python连接MySQL/redis/mongoDB数据库的简单整理
    docker安装和使用
    【Python】Django2.0集成Celery4.1详解
    django-haystack全文检索详细教程
    ubuntu16.04安装FastDFS-5.08
    redis之django-redis
    uva 1152 4 Values whose Sum is 0
    2015暑假acm短训小结
    Uva 12569 Planning mobile robot on Tree (EASY Version)
  • 原文地址:https://www.cnblogs.com/Ao-min/p/13880381.html
Copyright © 2011-2022 走看看