Hi there guys, ive been struggling all the weekend over the following issue. i have an array of objects that represents accountance values and amounts.Here is the structure of an item of that array:
objt:{
ref:12345,
amount:452012,
typeOp:"FRAAC",
compteD:675000,
compteC:null
}
Some of those objects share the same properties.i would like to be able to get those objects which share the same ref,typeOp and CompteD or CompteC, find their amounts and sum them up and get a new object inside an new array of objects.
To illustrate let’s say i have the following array:
[{ref:123,amount:35000,typeOp:"AAC",compteD:233000,compteC:null},{ref:123,amount:250000,typeOp:"FRAAC",compteD:675000,compteC:null},{ref:123,amount:3240,typeOp:"FRAAC",compteD:675000,compteC:null},{ref:123,amount:20000,typeOp:"FRAAC",compteD:675000,compteC:null},{ref:123,amount:35000,typeOp:"AAC",compteD:null,compteC:562115},{ref:123,amount:250000,typeOp:"FRAAC",compteD:null,compteC:562115},{ref:123,amount:3240,typeOp:"AAC",compteD:null,compteC:562115},{ref:123,amount:20000,typeOp:"AAC",compteD:null,compteC:562115},]
i’d like to reduce this array based on the typeOp the ref and the compteD or compteC. the end result should be the following:
[{ref:123,amount:35000,typeOp:"AAC",compteD:233000,compteC:null},{ref:123,amount:273240/*which is the sum of all the objects of the previous array which share the same typeOp,compteD and ref number*/,typeOp:"AAC",compteD:675000,compteC:null},{ref:123,amount:35000,typeOp:"AAC",compteD:null,compteC:562115},{ref:123,amount:273240/*which is the sum of all the objects of the previous array which share the same typeOp,compteC and ref number*/,typeOp:"FRAAC",compteD:null,compteC:562115}]
Can you help me figure this out as the array could have different ref numbers(a ref numbers identifies a particular operation).
Thanks.