js对象数组相同属性合并值相加,不知道代码那里有问题

原始数据
var obj=[
{
date:"2017-08-11",
state:"上",
result:{"温度":4,"湿度":3},
},
{
date:"2017-08-11",
state:"上",
result:{"温度":4,"湿度":3}
},
{
date:"2017-08-11",
state:"下",
result:{"温度":2,"湿度":3}
},
{
date:"2017-08-10",
state:"下",
result:{"温度":5,"湿度":3}
}
];
希望得到的结果
var result=[
{

date:"2017-08-11",
state:"上",
result:{"温度":8,"湿度":6}

},
{

date:"2017-08-11",
state:"下",
result:{"温度":2,"湿度":3}

},
{

date:"2017-08-10",
state:"下",
result:{"温度":5,"湿度":3}

},
]
代码如下:
var temp=[];
for(var i=0;i<obj.length;i++){
// console.log(obj[i]);
var test={};
if(i+1<obj.length){

if(obj[i].date===obj[i+1].date && obj[i].state===obj[i+1].state){
    test.date=obj[i].date;
    test.state=obj[i].state;
    test.result={};
    for(var key in obj[i].result){
        console.log(obj[i].result[key],obj[i+1].result[key]);
        test.result[key]=obj[i].result[key]+obj[i+1].result[key];
    }
    temp.push(test)
    i++;
}else{
    test.date=obj[i].date;
    test.state=obj[i].state;
    test.result=obj[i].result;
    temp.push(test)
}

}
}
运行之后的结果只有两个,少了最后一个,不知道问题出在那里?还望大神指点

阅读 8.6k
3 个回答
var obj=[
    {
        date:"2017-08-11",
        state:"上",
        result:{"温度":4,"湿度":3},
    },
    {
        date:"2017-08-11",
        state:"上",
        result:{"温度":4,"湿度":3}
    },
    {
        date:"2017-08-11",
        state:"下",
        result:{"温度":2,"湿度":3}
    },
    {
        date:"2017-08-10",
        state:"下",
        result:{"温度":5,"湿度":3}
    }
];


var temp = [];

obj.forEach(function(item, index) {
    
    var skey = item.date + item.state;
    
    if(typeof temp[skey] == "undefined") {
        temp[skey] = item;
    } else {
        for(var k in item.result) {
            temp[skey]["result"][k] += item["result"][k];
        }
        //temp[skey]["result"]["温度"] += item["result"]["温度"];
        //temp[skey]["result"]["湿度"] += item["result"]["湿度"];
    }
    
});
var result = [];
for(var i in temp) {
   result.push(temp[i]);
}
console.log(result);

试着写了下感觉代码挺麻烦.

你数据第三个日期8-11 第四个日期8-10。数据只有两个的原因,应该在 if(i+1 < obj.length)判断时,进行第四个这块代码没进行。 故temp只有两个

更新*
中午抽空写的代码:数据在temp里

var temp = [];
obj.reduce(function(prev, cur, index, array){
    var test = {};
    if( prev.date === cur.date && prev.state===cur.state){
        test.date=prev.date;
        test.state=prev.state;
        test.result={};
         for(var key in prev.result){
            test.result[key] = prev.result[key]+cur.result[key];
        }
        temp.push(test);
    }else if(array[index + 1].date === cur.date && array[index + 1].state===cur.state){
        return cur;
    }else{
        temp.push(cur);
    }
    return cur;
});

按你的思路,if(i+1<obj.length) 数组最后一个元素不满足条件,所以就被干掉了:

    var temp=[];
for(var i=0;i<obj.length;i++){
    // console.log(obj[i]);
    var test={};
    if(i+1<obj.length){  
    
        if(obj[i].date===obj[i+1].date && obj[i].state===obj[i+1].state){
            test.date=obj[i].date;
            test.state=obj[i].state;
            test.result={};
            for(var key in obj[i].result){
                console.log(obj[i].result[key],obj[i+1].result[key]);
                test.result[key]=obj[i].result[key]+obj[i+1].result[key];
            }
            temp.push(test)
            i++;
        }else{
            test.date=obj[i].date;
            test.state=obj[i].state;
            test.result=obj[i].result;
            temp.push(test)
        }
    
    }else{
        test.date=obj[i].date;
        test.state=obj[i].state;
        test.result=obj[i].result;
        temp.push(test)
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏