我有一个来自php文件的json输出,如下所示;
[{"device_id":"9700001","sensor_value":"31.5","update_time":"2017-04-28 18:49:06"},
{"device_id":"9700002","sensor_value":"31.5","update_time":"2017-04-28 18:47:05"},
{"device_id":"9700003","sensor_value":"31.5","update_time":"2017-04-28 18:45:05"},
{"device_id":"9700003","sensor_value":"33.1","update_time":"2017-04-28 06:24:57"},
{"device_id":"9700002","sensor_value":"33.1","update_time":"2017-04-28 06:22:57"},
{"device_id":"9700003","sensor_value":"33.1","update_time":"2017-04-28 06:20:56"},
{"device_id":"9700001","sensor_value":"33.1","update_time":"2017-04-28 06:18:56"},
{"device_id":"9700002","sensor_value":"33.1","update_time":"2017-04-28 06:16:56"},
{"device_id":"9700002","sensor_value":"33.1","update_time":"2017-04-28 06:14:56"}]
我想将此数组转换为特定数据集,即我想获取update_time的设备9700001的值和两个数据集的传感器值, 对于9700001,
datasensor1=[{"sensor_value":"31.5","update_time":"2017-04-28 18:49:06"},{"sensor_value":"33.1","update_time":"2017-04-28 06:18:56"},{"sensor_value":"33.1","update_time":"2017-04-28 06:18:56"}]
用于设备9700002,
datasensor2=[{"sensor_value":"31.5","update_time":"2017-04-28 18:47:05"},{"sensor_value":"33.1","update_time":"2017-04-28 06:22:57"},{"sensor_value":"33.1","update_time":"2017-04-28 06:16:56"},{"sensor_value":"33.1","update_time":"2017-04-28 06:14:56"}]
类似于设备9700003,
datasensor3=[{"sensor_value":"31.5","update_time":"2017-04-28 18:45:05"},{"9700003","sensor_value":"33.1","update_time":"2017-04-28 06:24:57"},{"sensor_value":"33.1","update_time":"2017-04-28 06:20:56"}]
我先厌倦了使用
按照device_id进行分组 var sensor= _.groupBy(data,"device_id");
但我之后无法将对象分开。我该如何解决这个问题?
答案 0 :(得分:0)
使用map()
和var data = [{"device_id":"9700001","sensor_value":"31.5","update_time":"2017-04-28 18:49:06"},
{"device_id":"9700002","sensor_value":"31.5","update_time":"2017-04-28 18:47:05"},
{"device_id":"9700003","sensor_value":"31.5","update_time":"2017-04-28 18:45:05"},
{"device_id":"9700003","sensor_value":"33.1","update_time":"2017-04-28 06:24:57"},
{"device_id":"9700002","sensor_value":"33.1","update_time":"2017-04-28 06:22:57"},
{"device_id":"9700003","sensor_value":"33.1","update_time":"2017-04-28 06:20:56"},
{"device_id":"9700001","sensor_value":"33.1","update_time":"2017-04-28 06:18:56"},
{"device_id":"9700002","sensor_value":"33.1","update_time":"2017-04-28 06:16:56"},
{"device_id":"9700002","sensor_value":"33.1","update_time":"2017-04-28 06:14:56"}];
var sorted = {};
data.forEach((row) => {
sorted[row.device_id] = [];
});
Object.keys(sorted).forEach((id) => {
sorted[id] = data.filter((row) => row.device_id === id).map((row) => ({
sensor_value: row.sensor_value,
update_time: row.update_time
}));
});
console.log(sorted);
:
Action [cursor]· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·

答案 1 :(得分:0)
尝试使用Array#reduce
方法
var data =[{"device_id":"9700001","sensor_value":"31.5","update_time":"2017-04-28 18:49:06"},
{"device_id":"9700002","sensor_value":"31.5","update_time":"2017-04-28 18:47:05"},
{"device_id":"9700003","sensor_value":"31.5","update_time":"2017-04-28 18:45:05"},
{"device_id":"9700003","sensor_value":"33.1","update_time":"2017-04-28 06:24:57"},
{"device_id":"9700002","sensor_value":"33.1","update_time":"2017-04-28 06:22:57"},
{"device_id":"9700003","sensor_value":"33.1","update_time":"2017-04-28 06:20:56"},
{"device_id":"9700001","sensor_value":"33.1","update_time":"2017-04-28 06:18:56"},
{"device_id":"9700002","sensor_value":"33.1","update_time":"2017-04-28 06:16:56"},
{"device_id":"9700002","sensor_value":"33.1","update_time":"2017-04-28 06:14:56"}]
function group(data , id){
return data.reduce((a,b)=>{
if(b.device_id == id)
a.push({
sensor_value:b.sensor_value,
update_time:b.update_time
})
return a
},[])
}
console.log(group(data,9700001))
console.log(group(data,9700002))