在JS中获得多数组的价值? [英] Getting value of multi array in JS?

查看:55
本文介绍了在JS中获得多数组的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在for循环中获取此数组中 id 的值?

How can I get the values of id in this array within a for loop?

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"
];

此外,如果我可以将其作为整数进行存储的方式来检索它,那就太好了.

Also, would be great if I could retrieve it as an integer, as the way it is being stored.

推荐答案

您可以使用此

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

如果您想要一个特定的代码,可以使用例如 image_array [2] .id ,它为您提供 2

If you want one specific you can use for example image_array[2].id which gives you 2

(看一下控制台)

这篇关于在JS中获得多数组的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆