我有这样的 JSON 数据
[
{"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}},
]
我想按 CITYID 对这些数据进行分组,并像这样对 SHAPELEN 求和。
[
{CITYID:2061, TOTAL=49.1},
{CITYID:2062, TOTAL=47.2},
{CITYID:2063, TOTAL=51,34}
]
我创建了一个 javascript 函数,但没有按我的意愿工作。
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);
}
});
}
有没有你看到的解决方案?