Home > Net >  TS/JS, Unable to sum more than one properties, using map set and get
TS/JS, Unable to sum more than one properties, using map set and get

Time:10-09

I have been trying to create a summary of an array of objects where it's grouped by the value of one property and 2 or more properties should get summed.

But for some reason the way I am trying is only giving me 2 values the property I am grouping by and first property I am summing.

I am unable to sum the next property.

The array I am starting with

combinedItems

[
  {
    itemSi: 1,
    productId: 'one',
    taxableValue: 100,
    taxValue: 10,
    product: { id: 'one', productName: 'product one', taxId: 'one' },
    tax: { id: 'one', taxName: 'tax one' }
  },
  {
    itemSi: 2,
    productId: 'two',
    taxableValue: 100,
    taxValue: 10,
    product: { id: 'two', productName: 'product two', taxId: 'one' },
    tax: { id: 'one', taxName: 'tax one' }
  }
]

I need to be able to group by the taxName and sum the taxableValue and taxValue.

const summaryValues = new Map<any []>();

 for(const {tax, taxableValue, taxValue} of combinedItems)
 

   summaryValues.set(
       tax.taxName,
       (summaryValues.get(tax.taxName) || 0)   taxableValue,
       (summaryValues.get(tax.taxName) || 0)   taxValue,
);

const summaries = [...summaryValues]
 console.log(summaries);
 
 const taxSummary = summaries.map(x => ({ 
  taxName: x[0], 
  taxableValue: x[1],
  taxValue: x[2]
}));

console.log(taxSummary)

The result I am getting

[ [ 'tax one', 200 ] ]

[ { taxName: 'tax one', taxableValue: 200, taxValue: undefined } ]

This is how the combined items are gotten:

const items: any[] = [
    {
        itemSi: 1,
        productId: "one",
        taxableValue: 100,
        taxValue: 10
    },
    {
        itemSi: 2,
        productId: "two",
        taxableValue: 100,
        taxValue: 10
    }
    ];
    
const products: any[] = [
    {
        id: "one",
        productName:"product one",
        taxId: "one"
    },
    {
        id: "two",
        productName:"product two",
        taxId: "one"
    }
    ]
    
const taxes: any[] = [
    {
        id: "one",
        taxName:"tax one"
    },
    {
        id: "two",
        taxName:"tax two"
    }
    ]
    
    let combinedItems: any [] = [] 
    
    combinedItems = items.map(x => {
        let pdtItem = products.find(z => z.id === x.productId);


    let taxItem = taxes.find(z => z.id === pdtItem.taxId);
    
          let item = {...x, product: {...pdtItem }, tax: {...taxItem}};
          return item;
        });

console.log(combinedItems)

CodePudding user response:

Map is a key-value store. What you're trying to do appears to be calling set with three arguments, whereas it only takes two (key and value).

If you need to produce multiple aggregations, you could store the results in an object:

const summaries = new Map();

for (const { tax: { taxName }, taxableValue, taxValue } of combinedItems) {
  const currentSummary = summaries.get(taxName) || { taxableValue: 0, taxValue: 0 }
  summaries.set(
    taxName, 
    { taxableValue: currentSummary.taxableValue   taxableValue, taxValue: currentSummary.taxValue   taxValue }
  );
}
  • Related