Javascript 整理对象数组中的相同键并汇总值

Javascript 整理对象数组中的相同键并汇总值,javascript,arrays,object,reduce,Javascript,Arrays,Object,Reduce,这是我的对象数组- var items=[{category:,sum:687355.25}, {类别:'',总和:526335.4}, {类别:“建筑、一般工程和粉刷”,总数:991844}, {类别:“非金属矿物的开采和采石”,总数:566317.64}, {类别:“批发:建筑用大理石”,金额:1100391.64}, {类别:“重型建筑承包、基础设施工程承包”,金额:600000}, {类别:“建筑和施工承包商”,总额:829142.67}, {类别:“建筑和施工承包商”,金额:9524

这是我的对象数组-

var items=[{category:,sum:687355.25},
{类别:'',总和:526335.4},
{类别:“建筑、一般工程和粉刷”,总数:991844},
{类别:“非金属矿物的开采和采石”,总数:566317.64},
{类别:“批发:建筑用大理石”,金额:1100391.64},
{类别:“重型建筑承包、基础设施工程承包”,金额:600000},
{类别:“建筑和施工承包商”,总额:829142.67},
{类别:“建筑和施工承包商”,金额:952417},
{类别:“建筑和施工承包商,一般”,总数:731128},
{类别:“建筑和施工承包商,一般”,总额:708000},
{类别:“建筑和施工承包商、机构和公共建筑”,总数:542540},
{类别:“零售:女装店”,总额:540000},
{类别:“零售:加油站”,总数:567000},
{类别:“融资——银行和外币”,总额:700000},
{类别:“融资——银行和支票结算机构”,总额:526950},
{类别:“房地产项目启动”,总数:1084839.77},
{类别:“房地产销售、租赁和租赁”,总额:650000},
{类别:“房地产服务采购代理”,金额:1147500},
{类别:“房地产开发”,总数:534000},
{category:“services:financial services”,sum:735000}]
您可以将Array#reduce与helper对象一起使用(
dict
)。辅助对象维护对已添加类别的引用。当对象包含新类别时,将其添加到数组和
dict
对象中。如果对象已存在于
dict
中,则将其总和添加到类别总和中

var items=[{“category”:“sum”:687355.25},{“category”:“sum”:526335.4},{“category”:“building,general work and whiteshing”,“sum”:991844},{“category”:“非金属矿产的开采和采石”,“sum”:566317.64},{“category”:“批发:建筑用大理石”,“sum”:1100391.64},{“category”:“重型建筑承包,基础设施工程承包商”,“金额”:600000},{“类别”:“建筑和施工承包商”,“金额”:829142.67},{“类别”:“建筑和施工承包商”,“金额”:952417},{“类别”:“建筑和施工承包商,一般”,“金额”:731128},{“类别”:“建筑和施工承包商,一般”,“金额”“:708000},{”类“:“建筑和施工承包商、机构和公共建筑”,“金额”:542540},{”类“:“零售:女装店”,“金额”:540000},{”类“:“零售:加油站”,“金额”:567000},{”类“:“融资-银行和外币”,“金额”:700000},{”类“:“融资-银行和支票结算机构”,“sum”:526950},{“类别”:“房地产项目启动”,“sum”:1084839.77},{“类别”:“房地产销售、租赁和租赁”,“sum”:650000},{“类别”:“房地产服务采购代理”,“sum”:1147500},{“类别”:“房地产开发”,“sum”:534000},{“类别”:“服务:金融服务”,“sum”:735000}”;
var dict=Object.create(null);//创建一个空对象
var结果=项目。减少(功能(arr,o){
var current=dict[o.category];//从dict获取对象
if(!current){//if dict不包含对象
current=Object.assign({},o);//创建对象的克隆-这可以防止更改原始对象
arr.push(当前);//将其推送到数组中
dict[o.category]=current;//将其添加到dict
}else{//如果dict包含对象
current.sum+=o.sum;//更新总和
}
返回arr;
}, []);
console.log(result);
您可以将Array#reduce与助手对象一起使用(
dict
)。帮助对象维护对已添加类别的引用。当对象包含新类别时,将其添加到数组和
dict
对象中。如果对象已存在于
dict
中,则将其总和添加到类别总和中

var items=[{“category”:“sum”:687355.25},{“category”:“sum”:526335.4},{“category”:“building,general work and whiteshing”,“sum”:991844},{“category”:“非金属矿产的开采和采石”,“sum”:566317.64},{“category”:“批发:建筑用大理石”,“sum”:1100391.64},{“category”:重型建筑承包,基础设施工程承包商,{“金额”:600000},{“类别”:“建筑和施工承包商”,“金额”:829142.67},{“类别”:“建筑和施工承包商”,“金额”:952417},{“类别”:“建筑和施工承包商,一般”,“金额”:731128},{“类别”:“建筑和施工承包商,一般”,“金额”“:708000},{”类“:“建筑和施工承包商、机构和公共建筑”,“金额”:542540},{”类“:“零售:女装店”,“金额”:540000},{”类“:“零售:加油站”,“金额”:567000},{”类“:“融资-银行和外币”,“金额”:700000},{”类“:“融资-银行和支票结算机构”,“sum”:526950},{“类别”:“房地产项目启动”,“sum”:1084839.77},{“类别”:“房地产销售、租赁和租赁”,“sum”:650000},{“类别”:“房地产服务采购代理”,“sum”:1147500},{“类别”:“房地产开发”,“sum”:534000},{“类别”:“服务:金融服务”,“sum”:735000}”;
var dict=Object.create(null);//创建一个空对象
var结果=项目。减少(功能(arr,o){
var current=dict[o.category];//从dict获取对象
if(!current){//if dict不包含对象
current=Object.assign({},o);//创建对象的克隆-这可以防止更改原始对象
arr.push(当前);//将其推送到数组中
dict[o.category]=current;//将其添加到dict
}else{//如果dict包含对象
current.sum+=o.sum;//更新总和
}
返回arr;
}, []);

console.log(result);
这确实可以使用reduce完成:

results = items.reduce(sumFunction)
这是值得一看的结构
results = items.reduce(sumFunction, []);
function sumFunction (accumulator, currentItem, currentIndex) {
    // look up if the current item is of a category that is already in our end result.
    index = accumulator.findIndex((item) => item.category === currentItem.category)
    if (index < 0) {
        accumulator.push(currentItem); // now item added to the array
    } else {
        accumulator[index].sum += currenItem.sum // update the sum of already existing item
    }
    return accumulator;
}
function sumFunction (accumulator, currentItem, currentIndex) {
    // look up if the current item is of a category that is already in our end result.
    index = accumulator.findIndex(function(item) { return (item.category === currentItem.category);});
    if (index < 0) {
        accumulator.push(currentItem); // now item added to the array
    } else {
        accumulator[index].sum += currenItem.sum // update the sum of already existing item
    }
    return accumulator;
}
var map = {};
var new_items = [];
var length=items.length;
for(var i=0;i<length;i++){
    if(items[i]["category"] in map){
        map[items[i]["category"]]+=items[i]["sum"];
        }
    else{
             map[items[i]["category"]]=items[i]["sum"];         
        }
    }
for(key in map){
    new_items.push({"category":key,"sum":map[key]});
   }