Question:
How to optimize a loop of merging objects and arrays in JS

Here we are optimizing a loop of merging objects and arrays in JS using reduce

private mergeData(dataArray) {

  const mergedData = {

    items: [],

    money: {

      money1: 0,

      money2: 0,

      money3: 0,

    }

  }


  return dataArray.reduce((acc, curr) => {

    acc.items.push(...curr.items)

    acc.money.money1 += curr.money.money1

    acc.money.money2 += curr.money.money2

    acc.money.money3 += curr.money.money3

    return acc;


  }, mergedData)


}


dataArray looks like an array of these objects

{

      items: [],

      money: {

        money1: 0,

        money2: 0,

        money3: 0,

      }

    }


Suggested blogs:

>Remove certain characters without losing formatting

>How to Automaing Power BI Report Generation in Python?

>Create a test case for add user functionality in a movie app - Python

>Avoid code duplication between django Q expression and Python

>How can I iterate over enum Flag alias?

>How to train a deep learning in case validation with Pytorch result NaN or high loss?s

>Fixing QVariant output issue in Python

>Removing ASCII formatted characters from my CAN messages in Python



Nisha Patel

Nisha Patel

Submit
0 Answers