gpt4 book ai didi

javascript 数组无法正确打印到控制台

转载 作者:行者123 更新时间:2023-11-30 10:41:16 25 4
gpt4 key购买 nike

我将一些日期放入数组中,并希望该数组中的第二个“层”包含与该日期关联的订单号。它似乎工作正常,但是当我在 chrome 控制台中打印出数组时,我只看到第一个日期数组。如果我手动访问 console.log(dateArray[1]['order_number']);,我会看到预期的信息。我没有正确构造这个数组吗?

谢谢!

    for ( var u in valuesArr ) {

//store the text seperator between the information and the order it is associated with
var sep = "?order_num=";

//store the string from which the information and order number will be extracted
var str = valuesArr[u];

//location of seperator
var start = str.indexOf("?order_num=");

var order_number = str.substring(start+sep.length);

var current_date = valuesArr[u].substring(0, start);

//date goes into "current_date" array
current_date = current_date.split(" / ");

//fashion it into a javascript date
//month needs one subtracted
dateArray[u] = new Date ( current_date[2], current_date[0]-1, current_date[1]);

/*
*to identify from which order this date derives, an order_number slot is created
*underneath the sorted date slot
*this is done so that when the html is reformatted to re-order the dates
*the content associated with this date can be 'brought along with' it
*as the its parent div's id contains the order number
*/
dateArray[u]['order_number'] = order_number;

}
console.log(dateArray)//only outputs the array of dates
console.log(dateArray[1]['order_number']);//outputs the order number

最佳答案

您对 dateArray[u]['order_number'] = order_number; 所做的是向 Date 对象添加属性,而不是向数组添加元素。

要向数组添加元素,请使用:

dateArray[u][1] = order_number;

关于javascript 数组无法正确打印到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10902981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com