0

Here I'm trying to add number of array separately, but I'm not able to achieve the expected output. If I want to add total I would do flatMap and add them togeather. But I want add them separately for each array.

Below is the snippet for what I have tried

const data = [{
    "itemDetails": [{
      "sizeOfBag": 1,
      "numberOfBags": 1,
      "quantityInBag": 1.0
    }]
  },
  {

    "itemDetails": [{
        "sizeOfBag": 1,
        "numberOfBags": 1,
        "quantityInBag": 1.0
      },
      {
        "sizeOfBag": 10,
        "numberOfBags": 1,
        "quantityInBag": 1.0
      }
    ],

  }
]
const newData = data.map(f => f.itemDetails);
console.log(newData);

for (let i = 0; i <= newData.length; i++) {
  const addData = newData.reduce((acc, newData) => acc + newData[i].sizeOfBag, 0);
}
console.log(addData);

Expected Output: [1,11]

3
  • @hev1 Updated with expected output Commented Jul 9, 2020 at 5:39
  • You have the for loop syntax wrong, it should be ; between the parts of the header, not ,. Commented Jul 9, 2020 at 5:41
  • @Barmar Updated, still getting some error Commented Jul 9, 2020 at 5:42

3 Answers 3

1

You can use map and reduce.

const res = newData.map(arr=>arr.reduce((acc,curr)=>acc+curr.sizeOfBag,0));

Your attempt with the for loop is close, but you are looping past the last index and mixing up your index with property names.

for (let i = 0; i < newData.length; i++) {
  newData[i] = newData[i].reduce((acc, newData) => acc + newData.sizeOfBag, 0);
}
Sign up to request clarification or add additional context in comments.

4 Comments

One doubt here. We are using map twice, so why can't we use flatMap ?
We are only using map once. The result is a one dimensional array so there is no need to flatten it.
is it possible to get the output as same [{"itemDetails": [{'totalSizeOfBag': 0}]},{"itemDetails": [{'totalSizeOfBag': 11}]}]
I think that merits a new question.
1

You need to call reduce on the nested arrays, not the top-level newData array. You can combine all these calls using map to get an array of the results.

const data = [{
    "itemDetails": [{
      "sizeOfBag": 1,
      "numberOfBags": 1,
      "quantityInBag": 1.0
    }]
  },
  {

    "itemDetails": [{
        "sizeOfBag": 1,
        "numberOfBags": 1,
        "quantityInBag": 1.0
      },
      {
        "sizeOfBag": 10,
        "numberOfBags": 1,
        "quantityInBag": 1.0
      }
    ],

  }
]
const newData = data.map(f => f.itemDetails);
console.log(newData);

const addData = newData.map(d => d.reduce((acc, x) => acc + x.sizeOfBag, 0));

console.log(addData);

1 Comment

is it possible to get the output as same [{"itemDetails": [{'totalSizeOfBag': 0}]},{"itemDetails": [{'totalSizeOfBag': 11}]}]
1

Do it like this, run the reduce function inside your map:

data.map(f => f.itemDetails.reduce((acc, i) => acc + i.sizeOfBag, 0));

3 Comments

is it possible to get the output as same [{"itemDetails": [{'totalSizeOfBag': 0}]},{"itemDetails": [{'totalSizeOfBag': 11}]}]
Yes, it's possible. But that's not what you said you wanted as expected output.
Use {totalSizeOfBag: 0} as the 2nd argument to reduce, and (acc.totalSizeOfBag += i.sizeOfBag, acc) as the body of the arrow function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.