1

I have below object and i need counts from the byPerson array inside the object. Basically, i want to check the count in each byPerson array elements and get those properties as an array.

for e.g.

var Closed = [16, 0, 1, 43] // here 1st count object has 16 as closed, 2nd count object has no closed property (so we set it to 0), 3rd one has value 1 and 4th one has value 43

Similarly var Verify = [0, 5, 0, 1]

How can i achieve this calculation and stored the results in Closed and Verify variables as shown above.

{
  "byPerson": [
    {
      "personId": "1514903899",
      "firstName": "Yatish",
      "lastName": "Patel",
      "count": {
        "Closed": 16,
      }
    },
    {
      "personId": "1559363884",
      "firstName": "Samuel",
      "lastName": "Chacko",
      "count": {
        "Verify": 5
      }
    },
    {
      "personId": "297895805",
      "firstName": "Tim",
      "lastName": "Altman",
      "count": {
        "Closed": 1
      }
    },
    {
      "personId": "others",
      "firstName": "Others",
      "lastName": "",
      "count": {
        "Closed": 43,
        "Verify": 1
      }
    }
  ],
  "resultDateTime": "2021-04-23T12:14:33.901"
}

I tried this way

const closedValues= releaseData.byPerson.map(element => element.count["Closed"] === undefined ? 0: element.count["Closed"]);
const verifyValues= releaseData.byPerson.map(element => element.count["Verify"] === undefined ? 0: element.count["Verify"]);
    ```

But i guess it is not the optimal solution where i have to calculate for each one seperately. 
3
  • If you are using ES6 and you object is called "jsonData" : const byPerson = jsonData.byPerson; const Closed = []; const Verify = []; for(let i=0; i<byPerson.length;++i) { Closed.push( (byPerson[i].count?.Closed) ? +byPerson[i].count.Closed : 0 ); Verify.push( (byPerson[i].count?.Verify) ? +byPerson[i].count.Verify: 0 ); } console.log(Closed, Verify); Commented Apr 27, 2021 at 18:52
  • 2
    @Zak- added my code which i tried. not the optimal way. Commented Apr 27, 2021 at 19:01
  • 2
    @ArenTrot -- Since the question is closed (I voted to re-open) .. I created a Fiddle for you HERE If that is what you're looking for .. I'll answer the question (with explanation) once re-opened Commented Apr 27, 2021 at 19:58

1 Answer 1

2

You can use Array.reduce to count this in one go:

const data = {
  "byPerson": [{
      "personId": "1514903899",
      "firstName": "Yatish",
      "lastName": "Patel",
      "count": {
        "Closed": 16,
      }
    },
    {
      "personId": "1559363884",
      "firstName": "Samuel",
      "lastName": "Chacko",
      "count": {
        "Verify": 5
      }
    },
    {
      "personId": "297895805",
      "firstName": "Tim",
      "lastName": "Altman",
      "count": {
        "Closed": 1
      }
    },
    {
      "personId": "others",
      "firstName": "Others",
      "lastName": "",
      "count": {
        "Closed": 43,
        "Verify": 1
      }
    }
  ],
  "resultDateTime": "2021-04-23T12:14:33.901"
}

const result = data.byPerson.reduce((result, {
  count
}) => {
  result.closed.push(count.Closed || 0);
  result.verify.push(count.Verify || 0);
  return result;
}, {
  closed: [],
  verify: []
});

console.log(result);

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

1 Comment

Thanks. This is much better than my way.

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.