使用Javascript / jQuery基于值动态拆分数组

时间:2017-12-04 09:25:33

标签: javascript jquery arrays dynamic split

我正在尝试根据JSON对象中内部数组的值动态分割数组。

假设我从OpenWeatherAPI收到所述数据:

var data = [
  {
    id: "1",
    date: "2017-11-27",
    type: "Meeting",
    log_in: "06:30:00",
    log_out: "11:50:00"
  },
  {
    id: "1",
    date: "2017-11-27",
    type: "Lunch",
    log_in: "12:00:00",
    log_out: "01:00:00"
  },
  {
    id: "1",
    date: "2017-11-27",
    type: "Seminar",
    log_in: "1:15:00",
    log_out: "03:05:00",
  },
  {
    id: "2",
    date: "2017-11-28",
    type: "Lunch",
    log_in: "12:00:00",
    log_out: "01:00:00"
   },
   {
      id: "2",
      date: "2017-12-28",
      type: "Out of the Office",
      log_in: "03:00:00",
      log_out: "05:00:00"
   }
];

var newObj = {};
for (var i = 0; i < data.length; ++i) {

    //if a obj for this id does not exist yet, create
    if (newObj[data[i].id] == undefined)
    {
       newObj[data[i].id] = {"id": data[i].id, "date": data[i].date, "logs": []};
    }

    //store log into logs array using obj id
    newObj[data[i].id].logs.push({"type" : data[i].type, "log_in" : data[i].log_in, "log_out" : data[i].log_out});
}

console.log(newObj);
 JSONObject jsonObject = new JSONObject(Your_Sring_data);
 String  id= jsonObject.getJsonString("id");
 String firstName= jsonObject.getJsonString("firstName");
 String lastName= jsonObject.getJsonString("lastName");
 String fullName= jsonObject.getJsonString("fullName");

如何根据 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>中的元素进行拆分?

这几乎是我手动将日期转换为YYYY-MM-DD格式,但我仍然在努力根据我得到的结果制作动态数组:

结果:

My result for the code above

编辑#1

我正在寻找的最终结果应该是这样的:

list

2 个答案:

答案 0 :(得分:0)

我完全不明白这个问题,但这就是你要找的东西:

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

答案 1 :(得分: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)
}