js数组对象相同项合并处理
js数组对象相同项合并处理
平时⼯作中会经常遇到数据处理,其中数组合并处理也会经常遇到,下⾯就是数组相同项合并的⼀种⽅式:
- 1.⾸先由原始的数组arr数据,
- 2.然后创建⼀个map空对象和⼀个dest空数组,通过判断map中是否含有某项来判断数组dest是否添加数据,- 3.然后再判断相同项和已有的dest数组内容⽐较合并;
 var arr = [
    {"id":"1","name":"张三","value":"1245"},
    {"id":"1","name":"张三","value":"1360"},
    {"id":"2","name":"李四","value":"1120"},
    {"id":"2","name":"李四","value":"1362"},
    {"id":"2","name":"李四","value":"1008"},
    {"id":"3","name":"王五","value":"1360"},
    {"id":"4","name":"赵六","value":"1986"},
    {"id":"4","name":"赵六","value":"1240"}
  ];
  var map = {},dest = [];
  for(var i = 0; i < arr.length; i++){
    var ai = arr[i];
    if(!map[ai.id]){
      dest.push({
        id: ai.id,
        name: ai.name,
        value: ai.value
js合并两个数组      });
      map[ai.id] = ai;
    }else{
      for(var j = 0; j < dest.length; j++){
        var dj = dest[j];
        if(dj.id == ai.id){
          dj.value=(parseFloat(dj.value) + parseFloat(ai.value)).toString();
          break;
        }
      }
    }
  };
  console.log(JSON.stringify(dest));
结果如下
[{"id":"1","name":"张三","value":"2605"},
{"id":"2","name":"李四","value":"3490"},
{"id":"3","name":"王五","value":"1360"},
{"id":"4","name":"赵六","value":"3226"}]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。