0

idforループ内でこの配列の値を取得するにはどうすればよいですか?

var image_array = [
    {image: 'images/candidates/1.jpg', link_url: 'images/candidates/1.jpg', link_rel: 'prettyPhoto', id: 0 }, 
        // image for the first layer, goes with the text from id="sw0"
    {image: 'images/candidates/2.jpg', link_url: 'images/candidates/2.jpg', link_rel: 'prettyPhoto', id: 1 },
        // image for the second layer, goes with the text from id="sw1"
    {image: 'images/candidates/3.jpg', link_url: 'images/candidates/3.jpg', link_rel: 'prettyPhoto', id: 2 },
        // image for the third layer, goes with the text from id="sw2"
    {image: 'images/candidates/4.jpg', link_url: 'images/candidates/4.jpg', link_rel: 'prettyPhoto', id: 3},
        // image for the fourth layer, goes with the text from id="sw3"
    {image: 'images/candidates/5.jpg', link_url: 'images/candidates/5.jpg', link_rel: 'prettyPhoto', id: 4},
        // image for the fifth layer, goes with the text from id="sw4"
    {image: 'images/candidates/6.jpg', link_url: 'images/candidates/6.jpg', link_rel: 'prettyPhoto', id: 5},
        // image for the sixth layer, goes with the text from id="sw5"
    {image: 'images/candidates/7.jpg', link_url: 'images/candidates/7.jpg', link_rel: 'prettyPhoto', id: 6},
        // image for the seventh layer, goes with the text from id="sw6"
    {image: 'images/candidates/8.jpg', link_url: 'images/candidates/8.jpg', link_rel: 'prettyPhoto', id: 7},
        // image for the eighth layer, goes with the text from id="sw7"
    {image: 'images/candidates/9.jpg', link_url: 'images/candidates/9.jpg', link_rel: 'prettyPhoto', id: 8},
        // image for the ninth layer, goes with the text from id="sw8"
    {image: 'images/candidates/10.jpg', link_url: 'images/candidates/10.jpg', link_rel: 'prettyPhoto', id: 9},
        // image for the tenth layer, goes with the text from id="sw9"
    {image: 'images/candidates/11.jpg', link_url: 'images/candidates/11.jpg', link_rel: 'prettyPhoto', id: 10}
        // image for the eleventh layer, goes with the text from id="sw10"
];

また、格納されている方法として、整数として取得できれば素晴らしいと思います。

4

2 に答える 2

2

これを使用できます:

for (i = 0; i<image_array.length;i++){
    console.log(image_array[i].id);
}

特定のものが必要な場合は、たとえば次のように使用できimage_array[2].idます。2

デモはこちら

(コンソールを見て)

于 2013-08-16T22:23:36.357 に答える
0

jquery を使用している場合、この美しさにより ID の配列が得られます。

var image_array_by_id = $.map(image_array, function (index) { return index.id });
于 2013-08-16T22:39:18.697 に答える