根据javascript中的另一个属性求和对象属性
我有一个类似的对象:根据javascript中的另一个属性求和对象属性,javascript,typescript,Javascript,Typescript,我有一个类似的对象: [ { origin: "XX", destination: "YY", volume: 500 }, { origin: "ZZ", destination: "YY", volume: 500 } ] 我想制作地图并减少这些对象,以获得某个属性(在我的案例中是destination)的卷的总和 所以我想得到一些类似于: [ { destination: "YY", volume
[
{
origin: "XX",
destination: "YY",
volume: 500
},
{
origin: "ZZ",
destination: "YY",
volume: 500
}
]
我想制作地图并减少这些对象,以获得某个属性(在我的案例中是destination)的卷的总和
所以我想得到一些类似于:
[
{
destination: "YY",
volume: 1000
}
]
谢谢。试试这个
变量数组=[{
产地:XX,
目的地:YY,
数量:500
},
{
产地:ZZ,,
目的地:YY,
数量:500
}
];
函数sumFna、propType、propVal{
var i=0;
array.foreachfunction{
如果[propType]&&a[propType]==propVal{
i+=a.体积;
}
};
console.log{
[propType]:propVal,
卷:i
};
}
sumFnarray,destination,YY 您正在尝试按目标属性对数组进行分组。这里是TypeScript中的一个常规groupBy函数,它可以创建一个映射ES2015或更高版本,如果需要,可以使用polyfill,如果知道该值始终是字符串属性,则可以使用普通对象替换该值,从目标值到目标值为该值的对象数组: 您可以将分组映射的条目数组映射到包含组目标和每个组的卷总和的对象数组:
const output = Array.from(groupBy("destination", input).entries())
.map(([dest, objs]) => ({
destination: dest,
volume: objs.reduce((acc, cur) => acc + cur.volume, 0)
}))
console.log(JSON.stringify(output)); // produces the desired output
希望有帮助;祝你好运
它适用于我,我现在正在使用v2.5.1 如果使用Array.reduce的强大功能,甚至可以提供与任何数据结构无关的通用基收集器方法,但使用它的第一个收集器或累加器参数来携带数据结构特定的getter/setter功能,以便操作此特定数据
正确操作OP给定示例代码的实现可能类似于以下实现
变量数据列表=[{
产地:XX,
目的地:YY,
数量:500
}, {
产地:ZZ,,
目的地:YY,
数量:500
}, {
产地:XX,
目的地:机管局,
数量:200
}, {
产地:ZZ,,
目的地:机管局,
数量:100
}];
函数CollectSourceValueConnectedTargetSamariesCollector,dataItem{//一种通用的数据结构无关摘要方法。。。
var registry=collector.registry;
var sourceValue=collector.getSourceValuedataItem;
var summaryItem=注册表[sourceValue];
如果!总结项{
summaryItem=registry[sourceValue]=Object.assign{},collector.summaryItem;
collector.putSummarySourcesummaryItem,sourceValue;
collector.summaryList.pushsummaryItem;
}
collector.SummaryTargetSummaryItem,collector.getTargetValuedataItem;
回程收集器;
}
var result=dataList.reducecollectSourceValueConnectedTargetSummaries,{/。
注册表:{},
//这两种方法只是读取数据,它们的实现反映了所操作的数据结构。
getSourceValue:函数dataItem{return dataItem.destination},
getTargetValue:函数dataItem{return dataItem.volume},
//接下来的两个方法控制映射过程,即所有摘要数据的输出。
putSummarySource:函数summaryItem,值{summaryItem.destination=value;},
SummaryTarget:函数summaryItem,值{summaryItem.volume=summaryItem.volume+value;},
summaryItem:{/*目的地:,*/卷:0},
摘要列表:[]
}.总结清单;
console.log'dataList:',dataList;
console.log'reduced dataList/result:',result;
.as控制台包装器{max height:100%!important;top:0;}可能重复感谢您的回答,但是代码没有在typescript中编译,因为知道我有版本2.5.3,我需要下载类型定义吗?要扩展.from和name映射,请在上面添加一个指向TypeScript的链接,以演示它在TypeScript中编译时不会出现问题。你说代码不能编译是什么意思?你有什么错误?或者它可以编译但不能在浏览器中运行?啊,抱歉,正如我上面提到的,代码需要ES2015或更高版本。在您的应用程序中,确保在lib中包含ES2015。
const input = [
{
origin: "XX",
destination: "YY",
volume: 500
},
{
origin: "ZZ",
destination: "YY",
volume: 500
}
]
const output = Array.from(groupBy("destination", input).entries())
.map(([dest, objs]) => ({
destination: dest,
volume: objs.reduce((acc, cur) => acc + cur.volume, 0)
}))
console.log(JSON.stringify(output)); // produces the desired output