Json解析并总结了json中的值

时间:2017-03-21 09:38:02

标签: javascript json

我有一组json值,如下所示

{"labels":[ "time", "softirq", "user", "system", "nice", "iowait" ], "data":[ [ 1490088352, 0, 14.64646, 3.53535, 0, 1.0101 ], [ 1490088351, 0, 27.77778, 3.0303, 0, 0 ], [ 1490088350, 0.49751, 12.93532, 2.98508, 0, 4.47761 ] }

我想解析这个json .sum标签{ "softirq", "user", "system", "nice", "iowait"}值并存储在名为“sum”的单独值中。我的最终输出应如下所示:

{ "time" : 1490088352 , "sum": "the value which we added "}
{"time" : 1490088351 , "sum": "the value which we added "}
{"time" : 1490088350 , "sum": "the value which we added "}

任何人都可以帮我解决这个问题吗?

6 个答案:

答案 0 :(得分:0)

使用Array#reduce将数组减少为单个值

Array.slice(1)将返回不包含第一个值的数组



var data = {
  "labels": ["time", "softirq", "user", "system", "nice", "iowait"],
  "data": [
    [1490088352, 0, 14.64646, 3.53535, 0, 1.0101],
    [1490088351, 0, 27.77778, 3.0303, 0, 0],
    [1490088350, 0.49751, 12.93532, 2.98508, 0, 4.47761]
  ]
};
var op = [];
data.data.forEach(function(el) {
  var obj = {
    time: el[0]
  };
  obj.sum = el.slice(1).reduce(function(a, b) {
    return a + b;
  });
  op.push(obj);
});
console.log(op);




答案 1 :(得分:0)

这个怎么样,首先加载数据,

var values= {
  "labels": ["time", "softirq", "user", "system", "nice", "iowait"],
  "data": [
    [1490088352, 0, 14.64646, 3.53535, 0, 1.0101],
    [1490088351, 0, 27.77778, 3.0303, 0, 0],
    [1490088350, 0.49751, 12.93532, 2.98508, 0, 4.47761]
  ]
};

然后,

var answer = values.data.map(function(d){return {"time":d[0], "sum": d.splice(1,d.length).reduce((a, b) => a + b, 0)};});

答案 2 :(得分:0)

    var source = {
  "labels": ["time", "softirq", "user", "system", "nice", "iowait"],
  "data": [
    [1490088352, 0, 14.64646, 3.53535, 0, 1.0101],
    [1490088351, 0, 27.77778, 3.0303, 0, 0],
    [1490088350, 0.49751, 12.93532, 2.98508, 0, 4.47761]
  ]
};

    var  data =source.data;


var mydata = [];
var eachObject={};

for(var i in data){
  sum=0;
  eachObject={};
  for(var j=1,l=data[i].length;j<l;j++ ){
    sum+=data[i][j];
  }

  eachObject.time=data[i][0];
  eachObject.sum=sum;
  mydata.push(eachObject);

  console.log(mydata);
}

答案 3 :(得分:0)

&#13;
&#13;



var arr= {"labels":[ "time", "softirq", "user", "system", "nice", "iowait" ], "data":[ [ 1490088352, 0, 14.64646, 3.53535, 0, 1.0101 ], [ 1490088351, 0, 27.77778, 3.0303, 0, 0 ], [ 1490088350, 0.49751, 12.93532, 2.98508, 0, 4.47761 ]] };
var op = [];
for(var i =0; i < arr.data.length; i++){
    var obj = {};
    obj.time = arr.data[i][0];
    obj.sum = 0;
    for(var j = 1; j < arr.data[i].length; j++){
        obj.sum += arr.data[i][j];
    }
    op.push(obj);
}
console.log(op);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

您可以使用一个数组,其中包含所需的sum标签和一个用于时间分组的哈希表。

&#13;
&#13;
var data = { "labels": ["time", "softirq", "user", "system", "nice", "iowait"], "data": [[1490088352, 0, 14.64646, 3.53535, 0, 1.0101], [1490088351, 0, 27.77778, 3.0303, 0, 0], [1490088350, 0.49751, 12.93532, 2.98508, 0, 4.47761]] },
    labels = ["softirq", "user", "system", "nice", "iowait"],
    result = data.data.reduce(function (hash) {
        return function (r, a) {
            if (!hash[a[0]]) {
                hash[a[0]] = { time: a[0], sum: 0 };
                r.push(hash[a[0]]);
            }
            hash[a[0]].sum += labels.reduce(function (r, l) { return r + (a[data.labels.indexOf(l)] || 0); }, 0);
            return r;
        };
    }(Object.create(null)),[]);

console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 5 :(得分:0)

    var data = {
      "labels": ["time", "softirq", "user", "system", "nice", "iowait"],
      "data": [
        [1490088352, 0, 14.64646, 3.53535, 0, 1.0101],
        [1490088351, 0, 27.77778, 3.0303, 0, 0],
        [1490088350, 0.49751, 12.93532, 2.98508, 0, 4.47761]
      ]
    };
    var op = [];
for(var i = 0; i < data.data.length; i++){
  var json = {"time" : data.data[i][0], "sum" : 0}
  for(var j = 1; j < data.data[i].length; j++){
        json.sum += data.data[i][j];
  }
   op.push(json)
}
console.log(op)