JavaScript:如何从多维数组中获取值? [英] JavaScript: How to Get the Values from a Multi-Dimensional Array?

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

问题描述

我正在尝试从多维数组中获取值。这就是我到目前为止所拥有的。

当我选择数组中的第一个选项时,我需要99的值和图像,例如Billy Joel。

I'm trying to get the values from a multi-dimensional array. This is what I have so far.
I need the value of 99 and the image when I select the first option in the array, e.g. "Billy Joel".

var concertArray = [
    ["Billy Joel", "99", "equal.png"],
    ["Bryan Adams", "89", "higher.png"],
    ["Brian Adams", "25", "lower.png"]
];

function populate(){
    for(i = 0; i < concertArray.length; i++){
        var select = document.getElementById("test");
        select.options[select.options.length] = new Option(concertArray[i][0], concertArray[i][1]);
    }
}


推荐答案

你可以尝试将多维数组转换为这样的对象数组:

You can try to transform the multi-dimensional array to an array of objects like this:

var concertArray = [
    {name: "Billy Joel", value: 99, image: "equal.png"},
    {name: "Bryan Adams", value: 89, image: "higher.png"},
    {name: "Brian Adams", value: 25, image: "lower.png"}
];

然后您可以像常规对象一样访问数组中的项目:

Then you can access the items in the array like regular objects:

var concertName = concertArray[0].name;
var concertPrice = parseFloat(concertArray[0].value);
var concertImage = concertArray[0].image;

这篇关于JavaScript:如何从多维数组中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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