想要使用Javascript将一个JSON对象拆分为多个JSON对象
Want to split one single JSON objects to multiple JSON objects using Javascript
我有一个JSON响应,如下所示,
[{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对象,如下所示。
[{label: "8"},{label: "9"},{label: "10"},…]
[{value: "1"},{value: "7"},{value: "12"},…]
[{value2: "0"},{value2: "2"},{value2: "1"},…] and more
我尝试了下面的事情,但没有成功。
Try1:
var o2 = { uhash: o.uhash };
delete o.uhash;
Try2:
使用for循环获取每个对、array.prush和JSON.stringy()方法。
我只想使用数据库的JSON响应创建一个堆叠的fusioncharts
您可以创建一个函数来返回所需的数组:然后,您可以映射并调用JSON:中的属性
function makeArray(value) {
return j.map(function(a) {
return {[value]: a[value]};
});
}
var labels = makeArray('label');
工作示例
您可以像这样使用reduce
并返回数组
var data = [{label: "8", value: "1", value2: "0", value3: "0"},{label: "9", value: "7", value2: "2", value3: "6"},{label: "10", value: "12", value2: "1", value3: "0"}]
var result = data.reduce(function(ar, e) {
ar[0] = (ar[0] || []).concat({label: e.label});
ar[1] = (ar[1] || []).concat({value: e.value});
ar[2] = (ar[2] || []).concat({value2: e.value2});
ar[3] = (ar[3] || []).concat({value3: e.value3});
return ar;
}, [])
console.log(result)
您还可以返回对象
var data = [{label: "8", value: "1", value2: "0", value3: "0"},{label: "9", value: "7", value2: "2", value3: "6"},{label: "10", value: "12", value2: "1", value3: "0"}],
keys = Object.keys(data[0]);
var result = data.reduce(function(ar, e) {
ar[keys[0]] = (ar[keys[0]] || []).concat({label: e.label});
ar[keys[1]] = (ar[keys[1]] || []).concat({value: e.value})
ar[keys[2]] = (ar[keys[2]] || []).concat({value2: e.value2})
ar[keys[3]] = (ar[keys[3]] || []).concat({value3: e.value3})
return ar;
}, {})
console.log(result)
为什么我不使用Array.prototype.map或Object.keys,因为在IE8中,这些方法不起作用。要使它们工作,需要在代码中添加Pollyfill。所以这里是你的解决方案,而不使用他们
var yourData = [
{ "label": "8", "value": "1", "value2": "0", "value3": "0" },
{ "label": "9", "value": "7", "value2": "2", "value3": "6" },
{ "label": "10", "value": "12", "value2": "1", "value3": "0" }
];
var convertData = function(data) {
var newData = [],
dataLnth = data.length;
//dynamically creating the array depending on arraySize
for(var i = 0; i<=dataLnth; i++)
newData.push([]);
//create the data
for(var index=0; index<dataLnth; index++) {
var counter = 1;
for(var key in data[index]) {
if(key === "label")
newData[0].push({"label" : data[index][key]});
else
newData[counter++].push({"value" : data[index][key]});
}
}
return newData;
};
//printing the output in the console
console.log(JSON.stringify(convertData(yourData)));
您可以使用Array#forEach()
并构建具有所需属性的新对象。
var array = [{ label: "8", value: "1", value2: "0", value3: "0" }, { label: "9", value: "7", value2: "2", value3: "6" }, { label: "10", value: "12", value2: "1", value3: "0" }],
keys = ['label', 'value', 'value2', 'value3'],
grouped = {};
array.forEach(function (a) {
keys.forEach(function (k) {
var o = {};
o[k] = a[k];
grouped[k] = grouped[k] || [];
grouped[k].push(o);
});
});
document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');
我喜欢这里的map()和getOwnPropertyNames(),尽管其他答案可能也一样好。
var data = [
{ "label": "8", "value": "1", "value2": "0", "value3": "0" },
{ "label": "9", "value": "7", "value2": "2", "value3": "6" },
{ "label": "10", "value": "12", "value2": "1", "value3": "0" }
];
var results = data.map(function(item) {
var retval = [];
Object.getOwnPropertyNames(item).forEach(function(val, idx, array) {
retval.push({ [val]: item[val] });
});
return retval;
});
console.log(JSON.stringify(results));
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 如何为json对象中的段发送array[]
- 将JSON对象传递给angular指令
- 更改JSON对象的结构
- 访问JSON对象内部的数组元素
- 在ejs-partial中对JSON对象进行迭代
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- 访问嵌套JSON对象的键,其中键是动态的
- json对象中缺少对象循环
- 发送json对象或使用express路由呈现视图
- 在play2框架中向json对象添加下拉列表项
- 元素名称上带有短划线 (-) 字符的 Json 对象
- autocomplete不接受源的json对象
- 如何在javascript中创建动态json对象
- 在使用客户端脚本时拾取JSON对象
- 如何通过json对象数组为嵌套对象赋值
- 构造JSON对象