1

I've got the following array

arr = [
  { 'Atencion Personalizada': 2, 'Caja': 3 },
  { 'Atencion Personalizada': 1 },
  { 'Tarifa Social': 3 }
]

Expected output: 9

And I would like to sum the properties the shortest way possible. The thing is that the object keys are always variable so i can't go for:

arr.reduce((acc,item) => acc+item.keyName)

Found this post but can't adapt the code to my solution either:

var data = [{ A: 4, B: 2 }, { A: 2, B: 1 }, { A: 3, B: 1 }, { A: 2, B: 1, C: 1 }],
    result = data.reduce((r, o) => (Object.entries(o).forEach(([k, v]) => r[k] = (r[k] || 0) + v), r), {});

Thank you in advance

1
  • 1
    @Andy answer i'm looking is 9 Commented Aug 31, 2021 at 20:16

7 Answers 7

3

Here's my solution. Map through the array and flatten the properties values, then reduce them.

const arr = [
  { 'Atencion Personalizada': 2, 'Caja': 3 },
  { 'Atencion Personalizada': 1 },
  { 'Tarifa Social': 3 }
]

console.log(arr.flatMap(e => Object.values(e)).reduce((a, b) => a + b));

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was loking for, thanks! It seem easier in my head than it really was
1

Use reduce twice, once on the outer array and once on the values of each object inside it.

const arr = [
  { 'Atencion Personalizada': 2, 'Caja': 3 },
  { 'Atencion Personalizada': 1 },
  { 'Tarifa Social': 3 }
];
const total = arr.reduce((gt, item) => 
  gt + Object.values(item).reduce((t, sub) => t + sub, 0)
, 0);

console.log(total);

Comments

0

Maybe something like this?

let acc = {};
for (let o of arr) for (let key in o) {
    acc[key] ??= 0;
    acc[key] += o[key];
}

Not a one-liner though

3 Comments

Yeah, thoght of that but I'm trying to improve my javascript skills. Thak's!
@juanmac, there's nothing wrong with making code make more sense. One-liners are difficult to comprehend.
Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.
0

You can do something like this:

total = arr.reduce((acc,item) => {
     const values = Object.values(item)
     values.forEach(item=> acc += item)
    return acc;
}, 0);

Check I passed the first value (0) as the second parameter to reduce method.

Comments

0

Hope this would help:

arr = [
  { 'Atencion Personalizada': 2, 'Caja': 3 },
  { 'Atencion Personalizada': 1 },
  { 'Tarifa Social': 3 }
]

 var sum = arr.map(item => {
    var x = 0
    for (const [key, value] of Object.entries(item)) {
        x+=value
    }
    return x
}).reduce( (acc, curr) => acc + curr)
console.log(sum)

Comments

0

arr = [
  { 'Atencion Personalizada': 2, 'Caja': 3 },
  { 'Atencion Personalizada': 1 },
  { 'Tarifa Social': 3 }
]
var total = 0;
arr.forEach((x, i) => 
    total += Object.values(x).reduce((a,b) => a+b)
);
console.log(total)

Comments

0

Solve it with this approach:

  • use JSON.stringify to get arr as string.
  • extract numbers with match with regex \d+ and flag g to return it as group.
  • map it to real numbers rather than string.
  • reduce it to the sum of all numbers.
JSON.stringify(arr) //'[{"Atencion Personalizada":2,"Caja":3},{"Atencion Personalizada":1},{"Tarifa Social":3}]'
.match(/\d+/g) //['2', '3', '1', '3']
.map(Number) //[2, 3, 1, 3]
.reduce((acc, n)=>acc+n, 0) //9

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.