2017-12-04 4 views
0

JSONオブジェクト内の内部配列の値に基づいて配列を動的に分割する簡単な方法を探しています。Javascript/jQueryを使用した値に基づいて配列を動的に分割する

var jsonData={ 
 
    "cod":"200", 
 
    "message":0.0895, 
 
    "cnt":5, 
 
    "list":[ 
 
     { 
 
     "dt":1512388800, 
 
     "main":{ 
 
      "temp":301.9, 
 
      "temp_min":299.858, 
 
      "temp_max":301.9, 
 
      "pressure":1017.53, 
 
      "sea_level":1020.58, 
 
      "grnd_level":1017.53, 
 
      "humidity":98, 
 
      "temp_kf":2.04 
 
     }, 
 
     "weather":[ 
 
      { 
 
       "id":803, 
 
       "main":"Clouds", 
 
       "description":"broken clouds", 
 
       "icon":"04n" 
 
      } 
 
     ], 
 
     "clouds":{ 
 
      "all":80 
 
     }, 
 
     "wind":{ 
 
      "speed":2.36, 
 
      "deg":161.003 
 
     }, 
 
     "rain":{ 
 

 
     }, 
 
     "sys":{ 
 
      "pod":"n" 
 
     }, 
 
     "dt_txt":"2017-12-04 12:00:00" 
 
     }, 
 
     { 
 
     "dt":1512399600, 
 
     "main":{ 
 
      "temp":300.75, 
 
      "temp_min":299.389, 
 
      "temp_max":300.75, 
 
      "pressure":1019.04, 
 
      "sea_level":1022.09, 
 
      "grnd_level":1019.04, 
 
      "humidity":100, 
 
      "temp_kf":1.36 
 
     }, 
 
     "weather":[ 
 
      { 
 
       "id":803, 
 
       "main":"Clouds", 
 
       "description":"broken clouds", 
 
       "icon":"04n" 
 
      } 
 
     ], 
 
     "clouds":{ 
 
      "all":68 
 
     }, 
 
     "wind":{ 
 
      "speed":2.04, 
 
      "deg":133.002 
 
     }, 
 
     "rain":{ 
 

 
     }, 
 
     "sys":{ 
 
      "pod":"n" 
 
     }, 
 
     "dt_txt":"2017-12-04 15:00:00" 
 
     }, 
 
     { 
 
     "dt":1512410400, 
 
     "main":{ 
 
      "temp":299.41, 
 
      "temp_min":298.726, 
 
      "temp_max":299.41, 
 
      "pressure":1017.89, 
 
      "sea_level":1020.93, 
 
      "grnd_level":1017.89, 
 
      "humidity":100, 
 
      "temp_kf":0.68 
 
     }, 
 
     "weather":[ 
 
      { 
 
       "id":801, 
 
       "main":"Clouds", 
 
       "description":"few clouds", 
 
       "icon":"02n" 
 
      } 
 
     ], 
 
     "clouds":{ 
 
      "all":24 
 
     }, 
 
     "wind":{ 
 
      "speed":2.08, 
 
      "deg":108.001 
 
     }, 
 
     "rain":{ 
 

 
     }, 
 
     "sys":{ 
 
      "pod":"n" 
 
     }, 
 
     "dt_txt":"2017-12-04 18:00:00" 
 
     }, 
 
     { 
 
     "dt":1512421200, 
 
     "main":{ 
 
      "temp":298.19, 
 
      "temp_min":298.19, 
 
      "temp_max":298.19, 
 
      "pressure":1017.39, 
 
      "sea_level":1020.45, 
 
      "grnd_level":1017.39, 
 
      "humidity":100, 
 
      "temp_kf":0 
 
     }, 
 
     "weather":[ 
 
      { 
 
       "id":801, 
 
       "main":"Clouds", 
 
       "description":"few clouds", 
 
       "icon":"02n" 
 
      } 
 
     ], 
 
     "clouds":{ 
 
      "all":24 
 
     }, 
 
     "wind":{ 
 
      "speed":2.26, 
 
      "deg":94.0002 
 
     }, 
 
     "rain":{ 
 

 
     }, 
 
     "sys":{ 
 
      "pod":"n" 
 
     }, 
 
     "dt_txt":"2017-12-04 21:00:00" 
 
     }, 
 
     { 
 
     "dt":1512432000, 
 
     "main":{ 
 
      "temp":298.444, 
 
      "temp_min":298.444, 
 
      "temp_max":298.444, 
 
      "pressure":1019.32, 
 
      "sea_level":1022.39, 
 
      "grnd_level":1019.32, 
 
      "humidity":100, 
 
      "temp_kf":0 
 
     }, 
 
     "weather":[ 
 
      { 
 
       "id":801, 
 
       "main":"Clouds", 
 
       "description":"few clouds", 
 
       "icon":"02d" 
 
      } 
 
     ], 
 
     "clouds":{ 
 
      "all":20 
 
     }, 
 
     "wind":{ 
 
      "speed":2.5, 
 
      "deg":89.0016 
 
     }, 
 
     "rain":{ 
 

 
     }, 
 
     "sys":{ 
 
      "pod":"d" 
 
     }, 
 
     "dt_txt":"2017-12-05 00:00:00" 
 
     } 
 
    ], 
 
    "city":{ 
 
     "id":1735158, 
 
     "name":"Petaling Jaya", 
 
     "coord":{ 
 
     "lat":3.1073, 
 
     "lon":101.6067 
 
     }, 
 
     "country":"MY" 
 
    } 
 
    } 
 

 
$.each(jsonData['list'], function(index, value) { 
 

 
       $.each(value, function(index, value){ 
 
       if(index == 'dt_txt') 
 
       { 
 
        var regExDate = new RegExp('^.{0,10}'); 
 
        value = regExDate.exec(value).toString(); 
 
        console.log(value); 
 
       } 
 
       }); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

私はdt_txtに基づいて分割するlistの要素を反復処理するにはどうすればよい:

はOpenWeatherAPIからデータを言った私は受けていますと仮定しますか?

これはかなり限り、私は手動でYYYY-MM-DD形式に日付を変換するようになったようですが、私はまだ私が取得していた結果のうち、ベースの動的配列を作るのに苦労しています:

結果:

My result for the code above

EDIT#1

私が探している最終的な結果は次のようになります。

var array20171204 = [{"dt":1512388800,"main":{"temp":301.9,"temp_min":299.858,"temp_max":301.9,"pressure":1017.53,"sea_level":1020.58,"grnd_level":1017.53,"humidity":98,"temp_kf":2.04},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":80},"wind":{"speed":2.36,"deg":161.003},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017-12-04 12:00:00"},{"dt":1512399600,"main":{"temp":300.75,"temp_min":299.389,"temp_max":300.75,"pressure":1019.04,"sea_level":1022.09,"grnd_level":1019.04,"humidity":100,"temp_kf":1.36},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":68},"wind":{"speed":2.04,"deg":133.002},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017-12-04 15:00:00"},{"dt":1512410400,"main":{"temp":299.41,"temp_min":298.726,"temp_max":299.41,"pressure":1017.89,"sea_level":1020.93,"grnd_level":1017.89,"humidity":100,"temp_kf":0.68},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":24},"wind":{"speed":2.08,"deg":108.001},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017-12-04 18:00:00"},{"dt":1512421200,"main":{"temp":298.19,"temp_min":298.19,"temp_max":298.19,"pressure":1017.39,"sea_level":1020.45,"grnd_level":1017.39,"humidity":100,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":24},"wind":{"speed":2.26,"deg":94.0002},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017-12-04 21:00:00"}]; 

var array20171205 = [{"dt":1512432000,"main":{"temp":298.444,"temp_min":298.444,"temp_max":298.444,"pressure":1019.32,"sea_level":1022.39,"grnd_level":1019.32,"humidity":100,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":20},"wind":{"speed":2.5,"deg":89.0016},"rain":{},"sys":{"pod":"d"},"dt_txt":"2017-12-05 00:00:00"}]; 
+1

あなたは最終的な結果は以下のようになります方法を指定できますか?あなたの質問は混乱しており、要点を逃しているようです。また、ライブラリを使って日付を解析するか、単に 'new Date()'を使うこともできます。 – danieltan95

+0

'私はまだ動的配列を作るのに苦労しています。これは、 'var arr = [];という単純なものにすることができます。 arr.push() 'を探していますが、最終的な配列構造がわからないと、推測が困難です。 – Nope

+0

こんにちは。私は期待した結果に疑念を明確にするためにいくつかの編集を追加しました。変数の名前は 'dt_time'の値に従って生成されることに注意してください。 したがって、データに対して日付に基づいて2D配列を動的に生成できる場合、これはX個の配列を保持する変数を生成する必要があるという問題を解決します。 –

答えて

0

は、私は完全に質問を理解していなかったが、これはあなたが探していたものです:私は、動的変数名を作成couldntの

var arr=[]; 
var list = jsonData['list']; 
list.filter(function(item) { 
    var temp = item.dt_txt.split(" ")[0]; 
    arr.push({[temp]: item}); 
}); 
+0

こんにちは。これはむしろ正確です。しかし、私はあなたがarrでそれを押している部分を理解しようとしています。 [temp]:itemは配列に挿入するときにキー/値のペアとして動作しますか? –

+0

はい、正確には、オブジェクトのキーが変数であるため、角カッコで囲んでいます – cdoshi

0

。このコードは他の側面を処理します。

var jsonData={ 
 
    "cod":"200", 
 
    "message":0.0895, 
 
    "cnt":5, 
 
    "list":[ 
 
     { 
 
     "dt":1512388800, 
 
     "main":{ 
 
      "temp":301.9, 
 
      "temp_min":299.858, 
 
      "temp_max":301.9, 
 
      "pressure":1017.53, 
 
      "sea_level":1020.58, 
 
      "grnd_level":1017.53, 
 
      "humidity":98, 
 
      "temp_kf":2.04 
 
     }, 
 
     "weather":[ 
 
      { 
 
       "id":803, 
 
       "main":"Clouds", 
 
       "description":"broken clouds", 
 
       "icon":"04n" 
 
      } 
 
     ], 
 
     "clouds":{ 
 
      "all":80 
 
     }, 
 
     "wind":{ 
 
      "speed":2.36, 
 
      "deg":161.003 
 
     }, 
 
     "rain":{ 
 

 
     }, 
 
     "sys":{ 
 
      "pod":"n" 
 
     }, 
 
     "dt_txt":"2017-12-04 12:00:00" 
 
     }, 
 
     { 
 
     "dt":1512399600, 
 
     "main":{ 
 
      "temp":300.75, 
 
      "temp_min":299.389, 
 
      "temp_max":300.75, 
 
      "pressure":1019.04, 
 
      "sea_level":1022.09, 
 
      "grnd_level":1019.04, 
 
      "humidity":100, 
 
      "temp_kf":1.36 
 
     }, 
 
     "weather":[ 
 
      { 
 
       "id":803, 
 
       "main":"Clouds", 
 
       "description":"broken clouds", 
 
       "icon":"04n" 
 
      } 
 
     ], 
 
     "clouds":{ 
 
      "all":68 
 
     }, 
 
     "wind":{ 
 
      "speed":2.04, 
 
      "deg":133.002 
 
     }, 
 
     "rain":{ 
 

 
     }, 
 
     "sys":{ 
 
      "pod":"n" 
 
     }, 
 
     "dt_txt":"2017-12-04 15:00:00" 
 
     }, 
 
     { 
 
     "dt":1512410400, 
 
     "main":{ 
 
      "temp":299.41, 
 
      "temp_min":298.726, 
 
      "temp_max":299.41, 
 
      "pressure":1017.89, 
 
      "sea_level":1020.93, 
 
      "grnd_level":1017.89, 
 
      "humidity":100, 
 
      "temp_kf":0.68 
 
     }, 
 
     "weather":[ 
 
      { 
 
       "id":801, 
 
       "main":"Clouds", 
 
       "description":"few clouds", 
 
       "icon":"02n" 
 
      } 
 
     ], 
 
     "clouds":{ 
 
      "all":24 
 
     }, 
 
     "wind":{ 
 
      "speed":2.08, 
 
      "deg":108.001 
 
     }, 
 
     "rain":{ 
 

 
     }, 
 
     "sys":{ 
 
      "pod":"n" 
 
     }, 
 
     "dt_txt":"2017-12-04 18:00:00" 
 
     }, 
 
     { 
 
     "dt":1512421200, 
 
     "main":{ 
 
      "temp":298.19, 
 
      "temp_min":298.19, 
 
      "temp_max":298.19, 
 
      "pressure":1017.39, 
 
      "sea_level":1020.45, 
 
      "grnd_level":1017.39, 
 
      "humidity":100, 
 
      "temp_kf":0 
 
     }, 
 
     "weather":[ 
 
      { 
 
       "id":801, 
 
       "main":"Clouds", 
 
       "description":"few clouds", 
 
       "icon":"02n" 
 
      } 
 
     ], 
 
     "clouds":{ 
 
      "all":24 
 
     }, 
 
     "wind":{ 
 
      "speed":2.26, 
 
      "deg":94.0002 
 
     }, 
 
     "rain":{ 
 

 
     }, 
 
     "sys":{ 
 
      "pod":"n" 
 
     }, 
 
     "dt_txt":"2017-12-04 21:00:00" 
 
     }, 
 
     { 
 
     "dt":1512432000, 
 
     "main":{ 
 
      "temp":298.444, 
 
      "temp_min":298.444, 
 
      "temp_max":298.444, 
 
      "pressure":1019.32, 
 
      "sea_level":1022.39, 
 
      "grnd_level":1019.32, 
 
      "humidity":100, 
 
      "temp_kf":0 
 
     }, 
 
     "weather":[ 
 
      { 
 
       "id":801, 
 
       "main":"Clouds", 
 
       "description":"few clouds", 
 
       "icon":"02d" 
 
      } 
 
     ], 
 
     "clouds":{ 
 
      "all":20 
 
     }, 
 
     "wind":{ 
 
      "speed":2.5, 
 
      "deg":89.0016 
 
     }, 
 
     "rain":{ 
 

 
     }, 
 
     "sys":{ 
 
      "pod":"d" 
 
     }, 
 
     "dt_txt":"2017-12-05 00:00:00" 
 
     } 
 
    ], 
 
    "city":{ 
 
     "id":1735158, 
 
     "name":"Petaling Jaya", 
 
     "coord":{ 
 
     "lat":3.1073, 
 
     "lon":101.6067 
 
     }, 
 
     "country":"MY" 
 
    } 
 
    } 
 

 
let dtArray = []; 
 
jsonData["list"].map((obj) => { 
 
    let dt = obj["dt_txt"].split(" ")[0] 
 
    if(dtArray.indexOf(dt) == -1) { 
 
     dtArray.push(dt) 
 
    } 
 
}); 
 
let tempArray; 
 
for(i=0 ;i<dtArray.length; i++) { 
 
    //let array+str(date) = []; 
 
    tempArray = []; 
 
    jsonData["list"].map((obj) => { 
 
     if(dtArray[i] == obj["dt_txt"].split(" ")[0]) { 
 
     tempArray.push(obj) 
 
     } 
 
    }); 
 
    console.log(tempArray) 
 
}

関連する問題