1

いくつかの日付を配列に入れています。その配列の 2 番目の「階層」に、その日付に関連付けられた注文番号を保持させたいと考えています。正しく動作しているように見えますが、クロム コンソールで配列を印刷すると、日付の最初の配列しか表示されません。手動でアクセスする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
4

3 に答える 3

2

使用しているdateArray[u]['order_number'] = order_number;のは、配列に要素を追加するのではなく、Dateオブジェクトにプロパティを追加することです。

配列に要素を追加するには、次を使用します。

dateArray[u][1] = order_number;
于 2012-06-05T18:49:14.007 に答える
2

開発者コンソールを暗黙的に信頼しないでください。標準がないため、「正しい」印刷はありません。

Chromeがオブジェクトにカスタムプロパティを表示しないことを決定した場合、それはChrome次第です。あなたの価値が明示的なテストでそこにあることが示されている限り、それが重要です。


FWIW、あなたはより望ましい結果を得るかもしれません...

console.dir(dateArray)

このconsole.dirメソッドは拡張可能なオブジェクトを提供するため、ドリルダウンしてカスタムプロパティを確認できます。

于 2012-06-05T18:49:34.400 に答える
0

valuesArrが配列の場合、これはオブジェクトの反復を実行するため、使用for (... in ...)は失敗します。したがって、、などの配列の他の要素を多数取得します。lengthindexOf

代わりに、従来のforループを使用してみてください。

for (var i = 0; i < valuesArr.length; i++) {
  // ...
}
于 2012-06-05T18:46:10.887 に答える