0

I have a JSON array returned from AJAX like this:

[{
    "country": "Italy",
    "year2004": 3.5,
    "year2005": 4.2
}, {
    "country": "London",
    "year2004": 1.7,
    "year2005": 3.1
}, {
    "country": "Brazil",
    "year2004": 2.8,
    "year2005": 2.9
}, {
    "country": "Spain",
    "year2004": 2.6,
    "year2005": 2.3
}, {
    "country": "France",
    "year2004": 1.4,
    "year2005": 2.1
}]

And I want to get the name of the array (In this case I want to get "country", "year2004", "year2005") not the value. Is there a way to do this by using jQuery/JavaScript?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
EriK
  • 113
  • 4
  • 1
    Use `Object.keys`, eg. `Object.keys(obj[0])` where `obj` is a reference to the object you've shown – Rory McCrossan Jul 13 '17 at 15:18
  • 1
    Possible duplicate of [Getting the objects property name](https://stackoverflow.com/questions/4260308/getting-the-objects-property-name) – Rory McCrossan Jul 13 '17 at 15:22
  • 1
    Possible duplicate of [Getting the object's property name](https://stackoverflow.com/questions/4260308/getting-the-objects-property-name) – Winter Jul 13 '17 at 15:22

2 Answers2

0

There are multiple possibilities in your case to do what you want. If you are storing the JSON in a variable, you can always use the + operator to concatenate a name to the JSON

myCustomJsonString = "Myarray: " + JsonString;

Alternatively, if you have parsed your JSON into an object, you can access the array with the Object.keys attribute.

var myArray = Object.keys(JsonObject[0]);

EDIT to answer second question:

What's happening is that you're overwriting the array every time you change the original object. To avoid this, just create a temp object and save that one to the array.

var temp = array;
res[i] = temp;
jared.nesbit
  • 126
  • 4
0

Thank you for your answers, I have resolved it by having array of "names". But I had a new problem:

I had to have an Object Array like this:

[{
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.9,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "data1"
}, {
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.9,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "data2"
},
 ......
]

Where data1,data2,... are dynamic values given by Names (Names is the array I was looking for), I'm trying by doing this:

var array = new Object();
var res = [];

for (i in Names)
{
    array["ballonText"] = "[[category]]: <b>[[value]]</b>";
    array["fillAlphas"] = 0.9;
    array["lineAlpha"] = 0.2;
    array["type"] = "column";
    array["valueField"] = Names[i];
    res[i] = array;
}
    result = res;

return result;

And I got it! I had what I'm looking for, but instead of have the Object Array I have this:

 [{
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.9,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "data2"
}, {
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.9,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "data2"
},
 ......
]

It's the same value for valueField, instead of have data1,data2,... I have data2,data2.. Could you please tell me why I'm having the same value for all the elements of my array? thank you!!

EriK
  • 113
  • 4