0

I would like to sum one specific value from my array of objects.

my array:

const myArray = [
{package_id: 1, width: "30"},
{package_id: 2, width: "20"},
{package_id: 3, width: "50"}
]

The value I would like to achive is the width. So the result would be 100 Is there any way to do this?

1 Answer 1

4

You can do it easily with a reduce function.

yourArray.reduce((acc, curr) => {
  return acc + parseInt(curr.width, 10);
}, 0);
Sign up to request clarification or add additional context in comments.

Comments

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.