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形式に日付を変換するようになったようですが、私はまだ私が取得していた結果のうち、ベースの動的配列を作るのに苦労しています:
結果:
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"}];
あなたは最終的な結果は以下のようになります方法を指定できますか?あなたの質問は混乱しており、要点を逃しているようです。また、ライブラリを使って日付を解析するか、単に 'new Date()'を使うこともできます。 – danieltan95
'私はまだ動的配列を作るのに苦労しています。これは、 'var arr = [];という単純なものにすることができます。 arr.push() 'を探していますが、最終的な配列構造がわからないと、推測が困難です。 –
Nope
こんにちは。私は期待した結果に疑念を明確にするためにいくつかの編集を追加しました。変数の名前は 'dt_time'の値に従って生成されることに注意してください。 したがって、データに対して日付に基づいて2D配列を動的に生成できる場合、これはX個の配列を保持する変数を生成する必要があるという問題を解決します。 –