2017-06-02 12 views
0

私は次のようにPHPファイルからjson出力を持っています。グループに応じて異なる変数に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"}] 

私は、私は2つのデータセット、9700001のため 、

デバイス9700002のため
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"}] 

にUPDATE_TIMEとセンサ値のデバイス9700001の値を取得したい特定のデータセットすなわちこの配列を変換したいですデバイス9700003のための同様

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"}] 

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"}] 

var sensor= _.groupBy(data,"device_id"); 

を使用してDEVICE_ID、グループに私が疲れて最初のが、私はその後、オブジェクトを分離することはできませんよ。この問題をどのように解決すればよいですか?

答えて

0

使用filter()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) => { 
 
\t 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);

+0

てみてください迅速な返信に感謝します。 –

+0

コードを動的オブジェクトの作成に変更しました –

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))

+0

迅速なreply.howに感謝します。 device_idの数に応じて、データセットを作成できるはずです。 –

関連する問題