简体   繁体   中英

How to get multiple values of associative Array in javascript

I have problem to getting multiple values from an associative array. I try to find the solution but most of them showing that how to get single value of key from the array.

var data = []
0 : 5 , 6 
1 : 2 , 4 
2 : 3 , 9  

Now I have to get value "5" and "6" from index[0] . How can I get it and separate them as variable?

It depends on how you stored your data.

If each value is a string, then you would have to do something like;

var data = ['5,6', '2,4', '3,9'];
var splitted = data[0].split(',');
var fiveStr = splitted[0]; // value will be a string
var sixStr = splitted[1]; // value will be a string

If the value is another array (better) then:

var data = [[5,6],[2,4],[3,9]];
var five = data[0][0]; // value will be a number
var six = data[0][1]; // value will be a number

Suppose you have multidimensional array like following then you can use this example-

    var data = []
    data[0] = new Array(5,6);
    data[1] = new Array(2,4);
    data[2] = new Array(3,9);


for(i=0;i<data.length;i++)
{
    for(j=0;j<data[i].length;j++)
    {
        alert(data[i][j]);
    }

}

To check example use this link- http://jsfiddle.net/rfu93Lv6/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM