【问题标题】:Splitting an array dynamically based on values using Javascript/jQuery使用 Javascript/jQuery 根据值动态拆分数组
【发布时间】: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


【解决方案1】:

我没有完全理解这个问题,但这就是你要找的:

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

【讨论】:

  • 嗨。这是比较准确的。但是,我正在尝试了解您在 arr 中推动它的部分。 [temp] : item 在插入数组时是否充当键/值对?
  • 没错,因为对象的键是一个变量,我们将它括在方括号中
【解决方案2】:

我无法创建动态变量名;除此之外,这段代码确实处理了其他方面;

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

【讨论】:

    猜你喜欢
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 2011-04-01
    相关资源
    最近更新 更多