based on this question Group array items using object
I quoted from this answer, answered by https://stackoverflow.com/users/1094311/1983
so I get data from this jquery then I parsed it, after that I push it to variable
the data that I get from myurl is like this
{"pname":"some1","datax":2.0643278,"prov":"11","datay":1},{"pname":"some1","datax":3.2142857142857144,"prov":"11","datay":1},{"pname":"some2","datax":1.125,"prov":"12","datay":1},{"pname":"some2","datax":1.6666666666666667,"prov":"12","datay":2}
this is how my array begin
$(document).ready(function(){
$.ajax({
url: "myurl",
method: "GET",
success: function(data) {
var parsedJSON = JSON.parse(data);
var result1 = [
(function(data) {
let tmp = [];
data.forEach(e => {
tmp.push({
type: "firstid",
prov: e.prov,
name: e.pname,
showInLegend:false,
dataPoints:[{x:e.datax, y:e.datay}],
})
});
return tmp;
})(parsedJSON)
];
from there I got result1[0] and if I console log it, it will be like this
[ { type: "firstid",
prov: 11,
name: some1,
showInLegend:false,
dataPoints:[{x:2.0643278, y:1}]
},
{ type: "firstid",
prov: 11,
name: some1,
showInLegend:false,
dataPoints:[{x:3.2142857142857144, y:1}]
},
{ type: "firstid",
prov: 12,
name: some2,
showInLegend:false,
dataPoints:[{x:1.125, y:1}]
},
{ type: "firstid",
prov: 12,
name: some2,
showInLegend:false,
dataPoints:[{x:1.6666666666666667, y:1}]
}]
I want to get some array to be like this when I console log it :
[ { type: "firstid",
prov: 11,
name: some1,
showInLegend:false,
dataPoints:[ {x:2.0643278, y:1},
{x:3.2142857142857144, y:1}]
},
{ type: "firstid",
prov: 12,
name: some2,
showInLegend:false,
dataPoints:[ {x:1.125, y:1},
{x:1.6666666666666667, y:1}]
}]
I spent my time trying to figure it out with array merge but it seems I can't figure it out, any help would be nice