Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  89] [ 1]  / answers: 1 / hits: 69393  / 12 Years ago, thu, august 2, 2012, 12:00:00

how can I push data into an array in js if it's type is likw this... d= [[label, value]]. At first I want to push the label data then the values....
I get the data from an xml file.
If I had only a simple array I used the simple variable.push sintax.
Will varialble[][0].push or variable[][1].push work


More From » arrays

 Answers
4

Maybe you would be better of using an object,


So you could do


var d  = {
"Label" : "Value"
};

And to add the value you could


d.label = "value";

This might be a more structured approach and easier to understand if your arrays become big. And if you build the JSON valid it's easy to make a string and parse it back in.


Like var stringD = JSON.stringify(d); var parseD = JSON.parse(stringD);


UPDATE - ARRAY 2D


This is how you could declare it


var items = [[1,2],[3,4],[5,6]];

alert(items[0][0]);

And the alert is reading from it,


To add things to it you would say items[0][0] = "Label" ; items[0][1] = "Value";


If you want to do all the labels then all the values do...


for(var i = 0 ; i < labelssize; i ++)
{
items[i][0] = labelhere;
}


for(var i = 0 ; i < labelssize; i ++)
{
items[i][1] = valuehere;
}

[#83909] Tuesday, July 31, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
khalilb

Total Points: 173
Total Questions: 110
Total Answers: 105

Location: Honduras
Member since Thu, Mar 23, 2023
1 Year ago
;