想要使用Javascript将一个JSON对象拆分为多个JSON对象

Want to split one single JSON objects to multiple JSON objects using Javascript

本文关键字:JSON 对象 一个 拆分 Javascript      更新时间:2023-09-26

我有一个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));