-1

次のようなアドレスを含むJavaScriptオブジェクトがあります

var addressList = [
    {"AddressID":"10011","AddressType":"Delivery","AddressLine1":"4 Caerleon Drive","AddressLine2":"Bittern","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 5LF","Country":"United Kingdom","ContactID":"10011"},
    {"AddressID":"10012","AddressType":"Home","AddressLine1":"526 Butts Road","AddressLine2":"Sholing","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"}
]

それに別の「アドレス」を追加したいので、次のようなものがあります

[
{"AddressID":"10011","AddressType":"Delivery","AddressLine1":"4 Caerleon Drive","AddressLine2":"Bittern","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 5LF","Country":"United Kingdom","ContactID":"10011"},
{"AddressID":"10012","AddressType":"Home","AddressLine1":"526 Butts Road","AddressLine2":"Sholing","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"},
{"AddressID":"10013","AddressType":"Home","AddressLine1":"5436 Bfds Road","AddressLine2":"Sherly","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"}
]

これを行う方法がわかりませんか?

4

3 に答える 3

2

Array#push配列に新しいオブジェクトを追加するために使用します。

addressList.push({"AddressID":"10013","AddressType":"Home","AddressLine1":"5436 Bfds Road","AddressLine2":"Sherly","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"});
于 2012-09-19T16:48:38.420 に答える
1

あなたが何に遭遇しているのかは私にはわかりませんが、あなたが遭遇している可能性のある問題は、あなたが与える最初のスニペットが JavaScript オブジェクトであり、2 番目のスニペットが配列であることです。次のようにする必要があります。

var addressList = [];
addressList.push({"AddressID":"10011",...});

次に、forEach を使用してリストを反復処理できます。

addressList.forEach(function(address) {
    alert(address.AddressID);
    doSomethingWithAddress(address);
});

MDNで配列の詳細を読むことができます

于 2012-09-19T16:55:58.610 に答える
0

addressList.push({AddressID: 1234, Country: 'Foo', ...});

于 2012-09-19T16:48:36.047 に答える