0

I have some json data, that looks like :

[{
"key": "Jan",
  "value": "400"
}, {
  "key": "Apr",
  "value": "500"
}, {
  "key": "Aug",
  "value": "24058.635"
}, {
  "key": "Sep",
  "value": "2160"
}, {
  "key": "Nov",
  "value": "115425"
}, {
  "key": "Dec",
  "value": "32570"
}]

I need to convert this to a key value pair array that should be like

[
  [Jan, 400],
  [Apr, 500],
  [Aug, 24058.635],
  [Sep, 2160],
  [Nov, 115425],
  [Dec, 32570]
]

Somebody please help me with Jquery or Javascript code to do this conversion.

3

4 Answers 4

3

You can use map() function to iterate on them and return an array you want.

let json =  [{"key":"Jan","value":"400"},{"key":"Apr","value":"500"},{"key":"Aug","value":"24058.635"},{"key":"Sep","value":"2160"},{"key":"Nov","value":"115425"},{"key":"Dec","value":"32570"}];

let obj = json.map(item => [item['key'], item['value']]);

console.log(obj);

Sign up to request clarification or add additional context in comments.

7 Comments

console is showing json.map is not a function(…)
@AJIN, What look does your json obj has? In my case it is an array. If you have it like a string, then do let json = JSON.parse(yourString) and after that use map function
I am processing the response from an ajax method.
after parsing output is showing like [Array[2], Array[2], Array[2], Array[2], Array[2], Array[2]]
Create a fiddle and show your code.Or for first make sure you get the json object as mine
|
1

You could do something like this.

var input = [{
  "key": "Jan",
  "value": "400"
}, {
  "key": "Apr",
  "value": "500"
}, {
  "key": "Aug",
  "value": "24058.635"
}, {
  "key": "Sep",
  "value": "2160"
}, {
  "key": "Nov",
  "value": "115425"
}, {
  "key": "Dec",
  "value": "32570"
}];

var output = input.map(function(obj) {
  return [obj.key, obj.value]
});
console.log(output);

4 Comments

output is showing as [Array[2], Array[2], Array[2], Array[2], Array[2], Array[2]]
if you would like to display it, you could use JSON.stringify(output).
If you need to access, you should use output[someIndex][0] for Key and output[someIndex][1] for Value
i am not accessing the data at any index. I am passing this output on to a function where a graph will be generated with this data.
1

with forEach method .

var a= [{"key":"Jan","value":"400"},{"key":"Apr","value":"500"},{"key":"Aug","value":"24058.635"},{"key":"Sep","value":"2160"},{"key":"Nov","value":"115425"},{"key":"Dec","value":"32570"}];
var ans=[];
a.forEach(function(a){
ans.push([a["key"],a["value"]]);
})
console.log(ans);

with map method .

var a= [{"key":"Jan","value":"400"},{"key":"Apr","value":"500"},{"key":"Aug","value":"24058.635"},{"key":"Sep","value":"2160"},{"key":"Nov","value":"115425"},{"key":"Dec","value":"32570"}];

var ans=a.map(function(a){
return [a["key"],a["value"]];
})
console.log(ans);

2 Comments

both methods are showing output as : [Array[2], Array[2], Array[2], Array[2], Array[2], Array[2]]
@AJIN JSON.stringify(output) only to show output as string . but keep output as it it . output is correct.
0

Using Array map() method :

var jsonObj = [{
"key": "Jan",
  "value": "400"
}, {
  "key": "Apr",
  "value": "500"
}, {
  "key": "Aug",
  "value": "24058.635"
}, {
  "key": "Sep",
  "value": "2160"
}, {
  "key": "Nov",
  "value": "115425"
}, {
  "key": "Dec",
  "value": "32570"
}];

var newArr = jsonObj.map(function(item) {
  return [item['key'],item['value']];
});

console.log(newArr);

Using JavaScript for...in loop :

var jsonObj = [{
"key": "Jan",
  "value": "400"
}, {
  "key": "Apr",
  "value": "500"
}, {
  "key": "Aug",
  "value": "24058.635"
}, {
  "key": "Sep",
  "value": "2160"
}, {
  "key": "Nov",
  "value": "115425"
}, {
  "key": "Dec",
  "value": "32570"
}];

var newArr = [];
for (var i in jsonObj) {
  newArr.push([jsonObj[i].key,jsonObj[i].value]);
}

console.log(newArr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.