Question

I have an array like this.

[{"label":"2013-12-01","value":"2372"},{"label":"2014-01-01","value":"2458"},{"label":"2014-02-01","value":"1636"},{"label":"2014-03-01","value":"2527"},{"label":"2014-04-01","value":"2129"},{"label":"2014-05-01","value":"149"}]

I'm creating a line chart using highcharts.

Data excepted format for line chart is

[["2013-12-01",2372],["2014-01-01",2458],["2014-02-01",1636],["2014-03-01",2527],["2014-04-01",2129],["2014-05-01",149]]

I got no idea on how to do it.

Actually the result is from a ajax result.

I get the result in Object Object and I converted it using JSON.stringify. But after converting I get both label and value.

Someone help me out to get the data in the chart expected format.

Thanks in advance.

Était-ce utile?

La solution

You can map the current array and return each label inside it into a new array etc

var new_arr = $.map(arr, function(itm) { return [itm.label, itm.value]; });

Autres conseils

You could use JavaScript's native Array.map() function, similar to this:

var y = x.map(function(item){
    return [item.label, item.value];
})

DEMO - Using Array.map


You can use following code

var orgArr = [{"label":"2013-12-01","value":"2372"},{"label":"2014-01-01","value":"2458"},  {"label":"2014-02-01","value":"1636"},{"label":"2014-03-01","value":"2527"},{"label":"2014-04-01","value":"2129"},{"label":"2014-05-01","value":"149"}]
var newArr = []
$.each(orgArr, function( index, value ) {
  newArr.push([value.label, value.value]);
});
console.log(newArr)

Fiddle for above code

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top