从for循环创建数组对象

时间:2018-08-15 12:45:35

标签: javascript arrays for-loop

我编写了以下代码,尝试从obj变量中的数据中提取ScheduledArrivalTime字段中的时间。我想尝试将提取的时间数据推送到新数组中,以便以后可以在其他函数中使用。

此刻,它正在将时间输出到我的数组中

05, 29, 00,05, 35, 00

但我想以

的形式获取
[{05, 29, 00}, {05, 35, 00}]

[{
    "time": [05, 29, 00]
}, {
    "time": [05, 35, 00]
}]

这是我目前获得的代码。当我尝试执行t.push({})时,所有代码输出均为[object,Object]。我不知道我要去哪里错了

var headings, obj, i, x = "";
obj = {
    "data": [{
        "Site": "RTL",
        "Operator": "RGB",
        "LineRef": "53a",
        "ScheduledArrivalTime": "2018-08-07 05:29:00",
        "EndPoint":"Madejski Stadium"
    }, {
        "Site": "RTL",
        "Operator": "RGB",
        "LineRef": "53a",
        "ScheduledArrivalTime": "2018-08-07 05:35:00",
        "EndPoint":"Madejski Stadium"
    }]
}


var t = [];
for (i in obj.data) {
    var time = obj.data[i].ScheduledArrivalTime[11] + obj.data[i].ScheduledArrivalTime[12] + ", " +  obj.data[i].ScheduledArrivalTime[14] + obj.data[i].ScheduledArrivalTime[15] + ", " +      obj.data[i].ScheduledArrivalTime[17] + obj.data[i].ScheduledArrivalTime[18];

 t.push(time);
}

document.getElementById("time").innerHTML = t;
<br>
<p id="time"></p>

    

2 个答案:

答案 0 :(得分:2)

我建议不要使用基于索引的小时/分钟/秒,而是从字符串中创建一个Date对象,然后再从中获取所有必需的信息。

您可以使用以下代码作为构建结构的示例,该结构内部具有小时/分钟/秒,以便以后用于格式化输出字符串。

var headings, obj, i, x = "";
obj = {
    "data": [{
        "Site": "RTL",
        "Operator": "RGB",
        "LineRef": "53a",
        "ScheduledArrivalTime": "2018-08-07 05:29:00",
        "EndPoint":"Madejski Stadium"
    }, {
        "Site": "RTL",
        "Operator": "RGB",
        "LineRef": "53a",
        "ScheduledArrivalTime": "2018-08-07 05:35:00",
        "EndPoint":"Madejski Stadium"
    }]
}

const retrieveScheduledArrivalTime = (data) => {
    return data.map((entry) => {
    const date = new Date(entry.ScheduledArrivalTime);
    const hours = date.getHours();
    const minutes = date.getMinutes();
    const seconds = date.getSeconds();
    return {
        hours,
      minutes,
      seconds
    };
    });
}

const getScheduledArrivalTimeAsText = (time) => {
    return time.hours + ':' + time.minutes + ':' + time.seconds;
};

const t = retrieveScheduledArrivalTime(obj.data);

document.getElementById("time").innerHTML = t.map(getScheduledArrivalTimeAsText).join('<br/>');

答案 1 :(得分:1)

好吧,例如,如果您想在innerHTML中写出确切的结构,例如,在console.log中看到,则需要显示一些字符。

如您所见,console.log在我的代码中准确显示了您所需的内容,但是要在文档中显示,我需要使用一种“ hacky”方式进行显示。

  

当我尝试执行t.push({})时,所有代码输出都是[object,Object]。

之所以这么说,是因为当您尝试将整个对象一次性添加到文档中时,它会使用对象的toString(),这就是为什么它显示[Object object]的原因,预期的行为。

请查看是否对您有帮助,否则请告诉我原因。

var headings, obj, i, x = "";
obj = {
    "data": [{
        "Site": "RTL",
        "Operator": "RGB",
        "LineRef": "53a",
        "ScheduledArrivalTime": "2018-08-07 05:29:00",
        "EndPoint":"Madejski Stadium"
    }, {
        "Site": "RTL",
        "Operator": "RGB",
        "LineRef": "53a",
        "ScheduledArrivalTime": "2018-08-07 05:35:00",
        "EndPoint":"Madejski Stadium"
    }]
}

var t = [];
for (i in obj.data) {
  var time = obj.data[i].ScheduledArrivalTime[11] + obj.data[i].ScheduledArrivalTime[12] + ", " +  obj.data[i].ScheduledArrivalTime[14] + obj.data[i].ScheduledArrivalTime[15] + ", " +      obj.data[i].ScheduledArrivalTime[17] + obj.data[i].ScheduledArrivalTime[18];
    
  let timeObj = {time} // <-- The Object you want is here.
  t.push(timeObj); 
}

console.log(t)
var myElem = document.getElementById("time")

//To show in the document exactly as an Array of objects structure:
myElem.innerHTML = "["

for (var i = 0; i < t.length; i++){
  myElem.innerHTML += "{" + t[i].time + "}"; 
  if (typeof t[i+1] != 'undefined'){
      myElem.innerHTML += ", "; 
  }
}

myElem.innerHTML += "]"
<p id="time"></p>