-1

Object

I have an Object of Arrays like this and I want to combine them(add values to every key )

It's possible to give to every array key: "state" and value: parseFloat(value) or how i can do it?

asked Jun 14, 2021 at 10:59
4
  • Use for loop or reduce to add up the values. Commented Jun 14, 2021 at 11:04
  • Does this answer your question? Sum values of objects in array Commented Jun 14, 2021 at 11:04
  • Isn't the title should be an array of objects? Commented Jun 14, 2021 at 11:08
  • 2
    Can you please update your question with the expected output and the attempt(s) you made of solving the problem? This way we can help you understand the issue better Commented Jun 14, 2021 at 11:11

1 Answer 1

1

You can save yourself some typing, and provide some future-proofing in case you later need to handle a different set of territories, by getting the keys from the object itself, and then iterating over those keys:

let val = {};
val = filteredMiles.reduce(
 function (previousValue, currentValue) {
 const keys=Object.keys(previousValue);
 const result={};
 keys.forEach(key=>{
 result[key]= parseFloat(previousValue[key])) 
 +parseFloat(currentvalue[key])
 })
 return result
 };
 });
console.log(val);
answered Jun 14, 2021 at 12:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.