使用$从json对象中提取相同键的和。每个循环jquery

时间:2021-06-24 20:12:04
"data": [{
  "Nic": {
    "2017-09":6473,
    "2017-10":66,
    "2017-11-01":69571
  },
  "Nancy": {
    "2017-09":185,
    "2017-10":194,
    "2017-11":2031
  }
}]

I want to get

我想要

series: [{
  dateValue: '2017-09',
  data: [6473, 185]
}, {
  dateValue: '2017-10',
  data: [66, 194]
}, {
  dateValue: '2017-11',
  data: [69571, 2031]
}]

2 个解决方案

#1


0  

Here you go with a solution

给你一个解

var data = [{
  "Nic": {
    "2017-09":6473,
    "2017-10":66,
    "2017-11":69571
  },
  "Nancy": {
    "2017-09":185,
    "2017-10":194,
    "2017-11":2031
  }
}];

var series = [];

var keys = Object.keys(data[0]);
var dates = Object.keys(data[0][keys[0]]);

$.each(dates, function(i){
  var temp = {};
  temp["dateValue"] = dates[i];
  temp["data"] = [];
  $.each(keys, function(j){
    temp["data"].push(data[0][keys[j]][dates[i]]);
  });
  series.push(temp);
});

console.log(series);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Hope this will help you.

希望这能对你有所帮助。

#2


0  

Here is the solution.

这是解决方案。

I used underscorejs, i would recommend you to use it. However you can easily convert this to jQuery.

我使用了underscorejs,我建议你使用它。但是,您可以很容易地将其转换为jQuery。

var data = [{
  "Nic": {
    "2017-09":6473,
    "2017-10":66,
    "2017-11":69571
  },
  "Nancy": {
    "2017-09":185,
    "2017-10":194,
    "2017-11":2031
  }
}];
var d = data[0];
var k = Object.keys(d);
var v = [];
_.forEach(k, function(ky){v.push(d[ky])});
var f = [];
_.forEach(v, function(vl){
	var kys = Object.keys(vl);
	_.forEach(kys, function(ky){
		var val =_.findWhere(f, {dateValue: ky});
        if(!val){
           val = {dateValue: ky, data: []};
           f.push(val);
        }
        val.data.push(vl[ky]);
	});
});
document.getElementById("val").innerHTML = JSON.stringify(f);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<div id="val">

</div>

#1


0  

Here you go with a solution

给你一个解

var data = [{
  "Nic": {
    "2017-09":6473,
    "2017-10":66,
    "2017-11":69571
  },
  "Nancy": {
    "2017-09":185,
    "2017-10":194,
    "2017-11":2031
  }
}];

var series = [];

var keys = Object.keys(data[0]);
var dates = Object.keys(data[0][keys[0]]);

$.each(dates, function(i){
  var temp = {};
  temp["dateValue"] = dates[i];
  temp["data"] = [];
  $.each(keys, function(j){
    temp["data"].push(data[0][keys[j]][dates[i]]);
  });
  series.push(temp);
});

console.log(series);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Hope this will help you.

希望这能对你有所帮助。

#2


0  

Here is the solution.

这是解决方案。

I used underscorejs, i would recommend you to use it. However you can easily convert this to jQuery.

我使用了underscorejs,我建议你使用它。但是,您可以很容易地将其转换为jQuery。

var data = [{
  "Nic": {
    "2017-09":6473,
    "2017-10":66,
    "2017-11":69571
  },
  "Nancy": {
    "2017-09":185,
    "2017-10":194,
    "2017-11":2031
  }
}];
var d = data[0];
var k = Object.keys(d);
var v = [];
_.forEach(k, function(ky){v.push(d[ky])});
var f = [];
_.forEach(v, function(vl){
	var kys = Object.keys(vl);
	_.forEach(kys, function(ky){
		var val =_.findWhere(f, {dateValue: ky});
        if(!val){
           val = {dateValue: ky, data: []};
           f.push(val);
        }
        val.data.push(vl[ky]);
	});
});
document.getElementById("val").innerHTML = JSON.stringify(f);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<div id="val">

</div>