javascript数组未正确打印到控制台

javascript数组未正确打印到控制台,javascript,Javascript,我将一些日期放入一个数组,并希望该数组中的第二个“层”保存与该日期关联的订单号。它似乎工作正常,但当我在chrome控制台中打印出数组时,我只看到第一个日期数组。如果我手动访问console.log(dateArray[1]['order_number']),我看到了预期的信息。我没有正确构造这个数组吗 谢谢 for ( var u in valuesArr ) { //store the text seperator between the information a

我将一些日期放入一个数组,并希望该数组中的第二个“层”保存与该日期关联的订单号。它似乎工作正常,但当我在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

如果
valuesArr
是一个数组,那么使用
for(…in…
将失败,因为这会进行对象迭代,因此会获取数组中的许多其他元素,如
长度
索引等

尝试使用传统的
for
循环:

for (var i = 0; i < valuesArr.length; i++) {
  // ...
}
for(变量i=0;i
如果
valuesArr
是一个数组,那么使用
for(…in…
将失败,因为这会进行对象迭代,因此会捕获数组中的许多其他元素,如
长度
索引等

尝试使用传统的
for
循环:

for (var i = 0; i < valuesArr.length; i++) {
  // ...
}
for(变量i=0;i
您使用的是
dateArray[u]['order\u number']=order\u number正在向日期对象添加属性,而不是向数组添加元素

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

dateArray[u][1] = order_number;

您使用的是
dateArray[u]['order\u number']=order\u number正在向日期对象添加属性,而不是向数组添加元素

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

dateArray[u][1] = order_number;

不要含蓄地信任开发人员控制台。没有“正确”的打印,因为没有标准

如果Chrome决定不在对象上显示自定义属性,则由Chrome决定。 只要通过显式测试显示出您的价值,这就是最重要的


FWIW,你可能会得到更理想的结果与

console.dir(dateArray)

console.dir
方法将为您提供可扩展的对象,以便您可以深入查看自定义属性。

不要隐式信任开发人员控制台。没有“正确”的打印,因为没有标准

如果Chrome决定不在对象上显示自定义属性,则由Chrome决定。 只要通过显式测试显示出您的价值,这就是最重要的


FWIW,你可能会得到更理想的结果与

console.dir(dateArray)

console.dir
方法将为您提供可扩展的对象,以便您可以深入查看自定义属性。

虽然您认为
for in
不是正确的工具,但它不会获取本机
数组。prototype
属性,如
.length
,因为它们是不可枚举的。只会遇到可枚举的扩展。@amnotiam肯定会更改为for循环。假设实际上是for-in循环会抓取这些其他元素,而for-in循环不会抓取这些元素,这是否正确?谢谢你帮我澄清。@thomas:
for in
枚举所有可枚举属性,不保证任何特定顺序。如果将可枚举属性添加到
Array.prototype
,则中的
也会遇到这些属性。标准的
for
循环对于数值索引的迭代是正确的,因为它强制执行特定的属性和顺序。虽然您认为
for in
不是正确的工具,但它不会获取本机
数组。原型
属性,如
.length
,因为它们是不可枚举的。只会遇到可枚举的扩展。@amnotiam肯定会更改为for循环。假设实际上是for-in循环会抓取这些其他元素,而for-in循环不会抓取这些元素,这是否正确?谢谢你帮我澄清。@thomas:
for in
枚举所有可枚举属性,不保证任何特定顺序。如果将可枚举属性添加到
Array.prototype
,则中的
也会遇到这些属性。标准的
for
循环对于数值索引的迭代是正确的,因为它强制特定的属性和顺序
与dateArray[u]相同。订单号=订单号
,这意味着对象
dateArray[u]
的属性
order\u number
被设置为变量
order\u number
@Wex的值,那么有没有办法给它一个字符串索引,或者它必须是一个数字索引?而且,我做这件事的方式(我并不是说它是好的或正确的)确实在chrome控制台中显示出了我所期望的效果。你知道为什么吗?谢谢。数组索引必须是数字。你所做的不一定是错的或坏的,只是可能不是你认为你在做的。控制台可能会说,“我这里有一个数组,所以我将循环遍历它……这个对象是一个日期……它有一个toString方法……所以我将调用该方法并显示结果。”它不会查找您可能添加到其中的随机属性。所以javascript中不能有“关联”数组?或者我对数组是“关联的”意味着什么感到困惑?我以为这只是一个字符串键..不,javascript中不能有关联数组。请稍等,我会找到一个参考链接。
dateArray[u]['order\u number']=order\u number
与dateArray[u]相同。订单号=订单号
,这意味着对象
dateArray[u]
的属性
order\u number
被设置为变量
order\u number
@Wex的值,那么有没有办法给它一个字符串索引,或者它必须是一个数字索引?而且,我做这件事的方式(我并不是说它是好的或正确的)确实在chrome控制台中显示出了我所期望的效果。A.