Home > Software engineering >  Best array of objects structure to sum values based on similar keys in a specific date range
Best array of objects structure to sum values based on similar keys in a specific date range

Time:07-07

I'm learning so please bear with me if it's a silly question. I created the two following array of objects structures:

var dictionary1Bis = [
    {
        "2022-07-06": [
            {
                material: "1_1$bolsa",
                quantity: 23
            },
            {
                material: "1_2$m3",
                quantity: 2
            },
            {
                material: "1_7$unidad",
                quantity: 4
            }
        ],
        "2022-07-07": [
            {
                material: "1_1$bolsa",
                quantity: 3
            }
        ],
        "2022-07-08": [
            {
                material: "1_1$bolsa",
                quantity: 10
            }
        ]
    }
];

and:

var dictionary1Ter = [
    {
        date: "2022-07-06",
        material: "1_1$bolsa",
        quantity: 23
    },
    {
        date: "2022-07-06",
        material: "1_2$m3",
        quantity: 2
    },
    {
        date: "2022-07-06",
        material: "1_7$unidad",
        quantity: 4
    },
    {
        date: "2022-07-07",
        material: "1_1$bolsa",
        quantity: 3
    },
    {
        date: "2022-07-08",
        material: "1_1$bolsa",
        quantity: 10
    }
];

I'm trying to understand which is the best (and easier to manipulate) structure to sum all the quantities of the objects for which both the following statements are TRUE:

  • date <= "2022-07-07";
  • material = "1_1$bolsa".

Basically what I would like to get is:

result = [
    {
        material: "1_1$bolsa",
        quantity: 26
    },
    {
        material: "1_2$m3",
        quantity: 2
    },
    {
        material: "1_7$unidad",
        quantity: 4
    },
];

I have the feeling that dictionary1Ter is easier to deal with, am I wrong? Is there any better array of objects structure to achieve what I'm trying to do? Also, any hints on the next steps would be much appreciated. Thank you!

CodePudding user response:

You could use a reduce operation to sum those materials

var dictionary1Ter = [
    {
        date: "2022-07-06",
        material: "1_1$bolsa",
        quantity: 23
    },
    {
        date: "2022-07-06",
        material: "1_2$m3",
        quantity: 2
    },
    {
        date: "2022-07-06",
        material: "1_7$unidad",
        quantity: 4
    },
    {
        date: "2022-07-07",
        material: "1_1$bolsa",
        quantity: 3
    },
    {
        date: "2022-07-08",
        material: "1_1$bolsa",
        quantity: 10
    }
];

var result = Object.values(
  dictionary1Ter.reduce((acc, el) => {
    if (el.date <= "2022-07-07") {
      if (!acc[el.material]) 
        acc[el.material] = {material: el.material, quantity: 0};
        
      acc[el.material].quantity  = el.quantity;
    }
    return acc;
  }, {})
);

console.log(result);

  • Related