1

I have below array of objects. As you can see, there is a nested object named count in each of these objects. I want to get total of Closed, Verify and Analyze

For e.g. the total of Closed is 23, Verify is 3 and Analyze is 20

"byPerson": [
      {
        "personId": "973567318",
        "firstName": "Others",
        "lastName": "",
        "count": {
          "Closed": 7,
          "Verify": 3,
          "Analyze": 19
        }
      },
      {
        "personId": "1514903899",
        "firstName": "Yatish",
        "lastName": "Patel",
        "count": {
          "Closed": 16,
          "Analyze": 1
        }
      }
 ]

I tried this way but it didnt work. for e.g to get total of Closed

cosnt result = data.reduce((a, b) => ({"Closed": a.count["Closed"] + b.count["Closed"]}));

3 Answers 3

2

const byPerson = [
      {
        "personId": "973567318",
        "firstName": "Others",
        "lastName": "",
        "count": {
          "Closed": 7,
          "Verify": 3,
          "Analyze": 19
        }
      },
      {
        "personId": "1514903899",
        "firstName": "Yatish",
        "lastName": "Patel",
        "count": {
          "Closed": 16,
          "Analyze": 1
        }
      }
 ];
const sum = byPerson.reduce((agg, item) => {
  ['Closed', 'Verify', 'Analyze'].forEach(f => agg[f] += item.count[f] || 0);
  return agg;
}, {Closed: 0, Verify: 0, Analyze: 0});
console.log(sum);

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

Comments

1

Reduce the data into an object with the keys for the nested properties you want to total. In the reduce callback destructure the "totals" properties and the nested count property and compute the summation.

const data = [
  {
    "personId": "973567318",
    "firstName": "Others",
    "lastName": "",
    "count": {
      "Closed": 7,
      "Verify": 3,
      "Analyze": 19
    }
  },
  {
    "personId": "1514903899",
    "firstName": "Yatish",
    "lastName": "Patel",
    "count": {
      "Closed": 16,
      "Analyze": 1
    }
  }
];

const totalCount = data.reduce(({
  Closed,
  Analyze,
  Verify
}, {
  count
}) => ({
  Closed: Closed + (count?.Closed || 0),
  Analyze: Analyze + (count?.Analyze || 0),
  Verify: Verify + (count?.Verify || 0),
}), {
  Closed: 0,
  Analyze: 0,
  Verify: 0,
});

console.log(totalCount);

6 Comments

Resse- i ran the code snippet but it gave me error.
@ArenTrot Ack, the "tidy" button changed the syntax. Sorry about that, it should be fixed now.
Resse- When i use this code, it tells me data.reduce is not a function
It returns 23, 20, 0, not 23, 20, 3
@ArenTrot I named the array data in my snippet, it may have a different name in your actual code.
|
1

This is a case where you really don't even need reduce and can use a simple loop as well as iterate the Object.keys()

const byPerson = [
      {
        "personId": "973567318",
        "firstName": "Others",
        "lastName": "",
        "count": {
          "Closed": 7,
          "Verify": 3,
          "Analyze": 19
        }
      },
      {
        "personId": "1514903899",
        "firstName": "Yatish",
        "lastName": "Patel",
        "count": {
          "Closed": 16,
          "Analyze": 1
        }
      }
 ];
 
const sum =  {Closed: 0, Verify: 0, Analyze: 0}

byPerson.forEach(({count})=> {
   Object.keys(sum).forEach(k => sum[k] += (count[k] || 0))
});
 

console.log(sum);

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.