简体   繁体   中英

javascript array not printing to console properly

I'm putting some dates into an array, and would like to have a second 'tier' in that array hold an order number associated with that date. It appears to be working correctly, but when I print out the array in the chrome console, I only see the first array of dates. If I access manually console.log(dateArray[1]['order_number']); , I see the expected information. Have I not constructed this array properly?

Thanks!

    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

What you are doing with dateArray[u]['order_number'] = order_number; is adding a property to the Date object, not adding an element to the array.

To add an element to the array, use:

dateArray[u][1] = order_number;

Don't trust developer consoles implicitly. There is no "correct" printing, because there's no standard.

If Chrome decides not to display the custom property on the object, that's up to Chrome. As long as your value is shown to be there with explicit testing, that's all that matters.


FWIW, you may get more desirable results with...

console.dir(dateArray)

The console.dir method will give you expandable objects so you can drill down and see your custom property.

If valuesArr is an array, then using for (... in ...) will fail since this does an object iteration and will therefore grab lots of other elements of the array like length , indexOf , etc. etc.

Try using the traditional for loop instead:

for (var i = 0; i < valuesArr.length; i++) {
  // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM