I have a JSON data like this
[
{"attributes":{"SHAPELEN":26.2293318917963,"CITYID":2061}},
{"attributes":{"SHAPELEN":16.9352548253636,"CITYID":2062}},
{"attributes":{"SHAPELEN":22.101151804600597,"CITYID":2063}},
{"attributes":{"SHAPELEN":22.8809188858315,"CITYID":2061}},
{"attributes":{"SHAPELEN":18.6906905910402,"CITYID":2063}},
{"attributes":{"SHAPELEN":31.322932493622,"CITYID":2062}},
{"attributes":{"SHAPELEN":10.5580020747299,"CITYID":2063}},
]
And I wanna group this data by CITYID and sum SHAPELENs like this.
[
{CITYID:2061, TOTAL=49.1},
{CITYID:2062, TOTAL=47.2},
{CITYID:2063, TOTAL=51,34}
]
I created a javascript function but did not work that I Want to.
function gropData(data) {
var features = data.features;
var cities = [];
$.each(features, function (index, item) {
// **if cities not include item, add in cities. But if not working here**
if (!$.inArray(item.attributes, cities) ) {
cities.push(item.attributes);
}
});
}
Is there a soluton that you see?
!$.inArray(item.attributes, cities)
supposed to do? Also$.inArray
returns an index so if you want to check for duplicate, you should do$.inArray == -1
– Vega Jul 19 '13 at 14:22