本文介绍了想要使用Javascript将一个JSON对象拆分为多个JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下JSON响应,
I have a JSON response like below,
[{label: "8", value: "1", value2: "0", value3: "0"},{label: "9", value: "7", value2: "2", value3: "6"},{label: "10", value: "12", value2: "1", value3: "0"},…]
现在我要拆分每个密钥分为4个JSON对象,如下所示。
Now I want to split it into 4 JSON objects for each key, like below.
[{label: "8"},{label: "9"},{label: "10"},…]
[{value: "1"},{value: "7"},{value: "12"},…]
[{value2: "0"},{value2: "2"},{value2: "1"},…] and more
我尝试了下面的事情,但没有成功。
I tried below things but wasn't successful.
试试1:
var o2 = { uhash: o.uhash };
delete o.uhash;
试试2:
使用for循环到得到每一对,array.push和JSON.stringigy()方法。
Using for loop to get each pair, array.push and JSON.stringigy() method.
我想要的是使用来自数据库的JSON响应创建堆叠的fusioncharts
All I want to is create a stacked fusioncharts using the JSON response from database
推荐答案
您可以创建一个返回所需数组的函数:
然后,您可以映射并在JSON中的属性上调用它:
You could make a function that returns the array you want:You could then map through and call this on the properties in your JSON:
function makeArray(value) {
return j.map(function(a) {
return {[value]: a[value]};
});
}
var labels = makeArray('label');
这篇关于想要使用Javascript将一个JSON对象拆分为多个JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!