-5

I have array data in this format , i want to get key and value array. how can be get key and value from this array in javascript .

jsonData =  [
    {"dimensions":[5.9,3.9,4.4,3.1,4.8],"icon":0,"curves":  [false,false,false,false,false],"id":"p1","color":"0x000000"},
    {"dimensions":[5.9,3.9,4.4,3.1,4.8],"icon":0,"curves":  [false,false,false,false,false],"id":"p1","color":"0x000000"},
    {"dimensions":[5.9,3.9,4.4,3.1,4.8],"icon":0,"curves":  [false,false,false,false,false],"id":"p1","color":"0x000000"}
];
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Mohit Gupta
  • 727
  • 2
  • 7
  • 21
  • 4
    Sorry to be blunt here, but what's the problem? JSON format IS an array. – Chris Dixon Apr 11 '13 at 09:30
  • hi thedixon i want to convert this data into array cause we neet to pass data into array format – Mohit Gupta Apr 11 '13 at 09:32
  • 1
    that json data is ALREADY a javascript array. – kennypu Apr 11 '13 at 09:33
  • 1
    That isn't JSON, the `jsonData = ` makes it JavaScript. The `[]` makes it an array. You already have a JavaScript array. So to echo thedixon, what is the problem? – Quentin Apr 11 '13 at 09:33
  • But this is an array format, in JavaScript - "jsonData" is your array. Is it because you don't know how to manipulate this array for your needs? – Chris Dixon Apr 11 '13 at 09:33
  • thedixon i understand please give me answer for this – Mohit Gupta Apr 11 '13 at 09:52
  • 1
    @mohit: We still don't know what the problem is though! Please explain what *result* you want. The more information you provide, the easier it is for us to help you. – Felix Kling Apr 11 '13 at 10:07
  • @Felix Kling i want to store key and value in array, means dimensions is key and value = 5.9,3.9,4.4,3.1,4.8 like all same – Mohit Gupta Apr 11 '13 at 10:11
  • @mohitgupta — The code in the question *already does that*! (Except that having named keys in an array doesn't make sense, but you've got that in an object and that object is in an array) – Quentin Apr 11 '13 at 10:12
  • 1
    @mohit: If you understand it, is the problem solved then? Because we don't understand. Anyways, I'm referring you to this question now: http://stackoverflow.com/q/11922383/218196. – Felix Kling Apr 11 '13 at 10:18
  • @Felix Kling i have change my code you look at now now problem is how to store in array – Mohit Gupta Apr 11 '13 at 10:23
  • @mohitgupta — Now you have changed your code … it is **still** stored in an array. – Quentin Apr 11 '13 at 10:24

2 Answers2

2

I have implement this solution

function getDataArray(name='', index=''){ 
   elements = generateDataArray("profiles");
   arrData = $.parseJSON(elements);  
   data = new Array();
   color = new Array();
   dataid = new Array();
   $.each(arrData,function(key,val){
       $.each(val,function(key2,val2){
     if(key2=="dimensions"){      
       data.push(val2)
      }else if(key2=="color"){
       color.push(val2)
       }

    })
    })

    number =0;
    newArray = new Array();
    while(number<data.length){
    newArray[number] = new Array(data[number],color[number])
    number++;
   }

      return newArray;
 }
Mohit Gupta
  • 727
  • 2
  • 7
  • 21
1

UPDATED: Added eval. Hadn't noticed that it was in a string.

It seems to me that your biggest problem is that it is all wrapped in an array with a single element. You can do:

var element = eval(jsonData)[0];

The eval is there to convert from string to a javascript object. Then, to access anything (for example the dimensions array), do the following:

var dimensions = element.dimensions;
Niels Abildgaard
  • 2,662
  • 3
  • 24
  • 32