【发布时间】:2018-05-17 17:23:21
【问题描述】:
我试图找到一种更简单的方法来根据 JSON 对象中的内部数组的值动态拆分数组。
假设我从 OpenWeatherAPI 接收到上述数据:
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>
如何迭代list 中的元素以根据dt_txt 进行拆分?
就我手动将日期转换为 YYYY-MM-DD 格式而言,这几乎是一样的,但我仍在努力根据我得到的结果制作一个动态数组:
结果:
编辑#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() -
I'm still struggling to make a dynamic array- 请描述阵列的外观。这可以像var arr = []; arr.push(<someRandomData>)一样简单,但如果不知道您要查找的最终数组结构是什么,就很难猜到。 -
嗨。我添加了一些编辑以澄清对我预期结果的一些疑问。请注意,变量的名称是根据
dt_time的值生成的。因此,如果我们可以根据数据动态生成二维数组,这将解决必须生成变量来保存 X 个数组的问题。
标签: javascript jquery arrays dynamic split