How do I calculate the number of the key of a specific property from a JSON object












-1















Image I have a



const events: {
'12327ae260fe9431': {
eventType: 'DEVICE_LINK_ERR',
key: '12327ae260fe9431',
severity: 'ERROR',
timestamp: 1542199782975,
title: 'Link went down unexpectedly',
},
'62d29fab77bddd7': {
eventType: 'DEVICE_LINK_ERR',
key: '62d29fab77bddd7',
severity: 'ERROR',
timestamp: 1542199783055,
title: 'Link went down unexpectedly',
},
},


I want to calculate the total severity and construct a severityCounts object like this



 severityCounts: {
CRITICAL: 0,
ERROR: 2,
INFO: 0,
WARNING: 0,
}


and I have access to Lodash. How should I go about doing this?










share|improve this question


















  • 2





    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

    – CertainPerformance
    Nov 22 '18 at 7:57






  • 1





    Start small...first figure out how to iterate through the object and access each severity. Once you get there it's fairly easy to add 1 to a counter for that type

    – charlietfl
    Nov 22 '18 at 8:03
















-1















Image I have a



const events: {
'12327ae260fe9431': {
eventType: 'DEVICE_LINK_ERR',
key: '12327ae260fe9431',
severity: 'ERROR',
timestamp: 1542199782975,
title: 'Link went down unexpectedly',
},
'62d29fab77bddd7': {
eventType: 'DEVICE_LINK_ERR',
key: '62d29fab77bddd7',
severity: 'ERROR',
timestamp: 1542199783055,
title: 'Link went down unexpectedly',
},
},


I want to calculate the total severity and construct a severityCounts object like this



 severityCounts: {
CRITICAL: 0,
ERROR: 2,
INFO: 0,
WARNING: 0,
}


and I have access to Lodash. How should I go about doing this?










share|improve this question


















  • 2





    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

    – CertainPerformance
    Nov 22 '18 at 7:57






  • 1





    Start small...first figure out how to iterate through the object and access each severity. Once you get there it's fairly easy to add 1 to a counter for that type

    – charlietfl
    Nov 22 '18 at 8:03














-1












-1








-1








Image I have a



const events: {
'12327ae260fe9431': {
eventType: 'DEVICE_LINK_ERR',
key: '12327ae260fe9431',
severity: 'ERROR',
timestamp: 1542199782975,
title: 'Link went down unexpectedly',
},
'62d29fab77bddd7': {
eventType: 'DEVICE_LINK_ERR',
key: '62d29fab77bddd7',
severity: 'ERROR',
timestamp: 1542199783055,
title: 'Link went down unexpectedly',
},
},


I want to calculate the total severity and construct a severityCounts object like this



 severityCounts: {
CRITICAL: 0,
ERROR: 2,
INFO: 0,
WARNING: 0,
}


and I have access to Lodash. How should I go about doing this?










share|improve this question














Image I have a



const events: {
'12327ae260fe9431': {
eventType: 'DEVICE_LINK_ERR',
key: '12327ae260fe9431',
severity: 'ERROR',
timestamp: 1542199782975,
title: 'Link went down unexpectedly',
},
'62d29fab77bddd7': {
eventType: 'DEVICE_LINK_ERR',
key: '62d29fab77bddd7',
severity: 'ERROR',
timestamp: 1542199783055,
title: 'Link went down unexpectedly',
},
},


I want to calculate the total severity and construct a severityCounts object like this



 severityCounts: {
CRITICAL: 0,
ERROR: 2,
INFO: 0,
WARNING: 0,
}


and I have access to Lodash. How should I go about doing this?







javascript json oop ecmascript-6 frontend






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 7:56









Zhenghao HeZhenghao He

937




937








  • 2





    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

    – CertainPerformance
    Nov 22 '18 at 7:57






  • 1





    Start small...first figure out how to iterate through the object and access each severity. Once you get there it's fairly easy to add 1 to a counter for that type

    – charlietfl
    Nov 22 '18 at 8:03














  • 2





    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

    – CertainPerformance
    Nov 22 '18 at 7:57






  • 1





    Start small...first figure out how to iterate through the object and access each severity. Once you get there it's fairly easy to add 1 to a counter for that type

    – charlietfl
    Nov 22 '18 at 8:03








2




2





The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

– CertainPerformance
Nov 22 '18 at 7:57





The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

– CertainPerformance
Nov 22 '18 at 7:57




1




1





Start small...first figure out how to iterate through the object and access each severity. Once you get there it's fairly easy to add 1 to a counter for that type

– charlietfl
Nov 22 '18 at 8:03





Start small...first figure out how to iterate through the object and access each severity. Once you get there it's fairly easy to add 1 to a counter for that type

– charlietfl
Nov 22 '18 at 8:03












3 Answers
3






active

oldest

votes


















2














You could group the data by severity, get the length and assign all information to a single object.






var data = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly' }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7', severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly' } },
result = _.assign(
{ CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 },
..._(data)
.values()
.groupBy('severity')
.map(({ length }, key) => ({ [key]: length }))
.value()
);

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








share|improve this answer

































    2














    You can use Object.values() to generate an array of all your values and then using array#reduce you can create an object with a count of each severity count.






    const events = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly', }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7',severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly', }, },
    severityCounts = Object.values(events).reduce((r, {severity}) => {
    r[severity] = ( r[severity] || 0 ) + 1;
    return r;
    },{ CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0});
    console.log(severityCounts);








    share|improve this answer































      1














      Please always post your attempt in OP before asking so that we get to know where you are stuck



      You can loop through the object using for .. of and increment counter like below. No need for lodash






      const events = {
      '12327ae260fe9431': {
      eventType: 'DEVICE_LINK_ERR',
      key: '12327ae260fe9431',
      severity: 'ERROR',
      timestamp: 1542199782975,
      title: 'Link went down unexpectedly',
      },
      '62d29fab77bddd7': {
      eventType: 'DEVICE_LINK_ERR',
      key: '62d29fab77bddd7',
      severity: 'ERROR',
      timestamp: 1542199783055,
      title: 'Link went down unexpectedly',
      },
      '62d29fab77bddd71': {
      eventType: 'DEVICE_LINK_ERR',
      key: '62d29fab77bddd7',
      severity: 'INFO',
      timestamp: 1542199783055,
      title: 'Link went down unexpectedly',
      }
      }

      let severityCounts = { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 }

      for(let { severity } of Object.values(events)) {
      severityCounts[severity] = severityCounts[severity] || 0
      severityCounts[severity]++
      }

      console.log(severityCounts)








      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426210%2fhow-do-i-calculate-the-number-of-the-key-of-a-specific-property-from-a-json-obje%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        You could group the data by severity, get the length and assign all information to a single object.






        var data = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly' }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7', severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly' } },
        result = _.assign(
        { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 },
        ..._(data)
        .values()
        .groupBy('severity')
        .map(({ length }, key) => ({ [key]: length }))
        .value()
        );

        console.log(result);

        .as-console-wrapper { max-height: 100% !important; top: 0; }

        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








        share|improve this answer






























          2














          You could group the data by severity, get the length and assign all information to a single object.






          var data = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly' }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7', severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly' } },
          result = _.assign(
          { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 },
          ..._(data)
          .values()
          .groupBy('severity')
          .map(({ length }, key) => ({ [key]: length }))
          .value()
          );

          console.log(result);

          .as-console-wrapper { max-height: 100% !important; top: 0; }

          <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








          share|improve this answer




























            2












            2








            2







            You could group the data by severity, get the length and assign all information to a single object.






            var data = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly' }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7', severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly' } },
            result = _.assign(
            { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 },
            ..._(data)
            .values()
            .groupBy('severity')
            .map(({ length }, key) => ({ [key]: length }))
            .value()
            );

            console.log(result);

            .as-console-wrapper { max-height: 100% !important; top: 0; }

            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








            share|improve this answer















            You could group the data by severity, get the length and assign all information to a single object.






            var data = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly' }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7', severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly' } },
            result = _.assign(
            { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 },
            ..._(data)
            .values()
            .groupBy('severity')
            .map(({ length }, key) => ({ [key]: length }))
            .value()
            );

            console.log(result);

            .as-console-wrapper { max-height: 100% !important; top: 0; }

            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








            var data = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly' }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7', severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly' } },
            result = _.assign(
            { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 },
            ..._(data)
            .values()
            .groupBy('severity')
            .map(({ length }, key) => ({ [key]: length }))
            .value()
            );

            console.log(result);

            .as-console-wrapper { max-height: 100% !important; top: 0; }

            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





            var data = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly' }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7', severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly' } },
            result = _.assign(
            { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 },
            ..._(data)
            .values()
            .groupBy('severity')
            .map(({ length }, key) => ({ [key]: length }))
            .value()
            );

            console.log(result);

            .as-console-wrapper { max-height: 100% !important; top: 0; }

            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 '18 at 8:26

























            answered Nov 22 '18 at 8:21









            Nina ScholzNina Scholz

            179k1491159




            179k1491159

























                2














                You can use Object.values() to generate an array of all your values and then using array#reduce you can create an object with a count of each severity count.






                const events = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly', }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7',severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly', }, },
                severityCounts = Object.values(events).reduce((r, {severity}) => {
                r[severity] = ( r[severity] || 0 ) + 1;
                return r;
                },{ CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0});
                console.log(severityCounts);








                share|improve this answer




























                  2














                  You can use Object.values() to generate an array of all your values and then using array#reduce you can create an object with a count of each severity count.






                  const events = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly', }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7',severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly', }, },
                  severityCounts = Object.values(events).reduce((r, {severity}) => {
                  r[severity] = ( r[severity] || 0 ) + 1;
                  return r;
                  },{ CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0});
                  console.log(severityCounts);








                  share|improve this answer


























                    2












                    2








                    2







                    You can use Object.values() to generate an array of all your values and then using array#reduce you can create an object with a count of each severity count.






                    const events = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly', }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7',severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly', }, },
                    severityCounts = Object.values(events).reduce((r, {severity}) => {
                    r[severity] = ( r[severity] || 0 ) + 1;
                    return r;
                    },{ CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0});
                    console.log(severityCounts);








                    share|improve this answer













                    You can use Object.values() to generate an array of all your values and then using array#reduce you can create an object with a count of each severity count.






                    const events = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly', }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7',severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly', }, },
                    severityCounts = Object.values(events).reduce((r, {severity}) => {
                    r[severity] = ( r[severity] || 0 ) + 1;
                    return r;
                    },{ CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0});
                    console.log(severityCounts);








                    const events = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly', }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7',severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly', }, },
                    severityCounts = Object.values(events).reduce((r, {severity}) => {
                    r[severity] = ( r[severity] || 0 ) + 1;
                    return r;
                    },{ CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0});
                    console.log(severityCounts);





                    const events = { '12327ae260fe9431': { eventType: 'DEVICE_LINK_ERR', key: '12327ae260fe9431', severity: 'ERROR', timestamp: 1542199782975, title: 'Link went down unexpectedly', }, '62d29fab77bddd7': { eventType: 'DEVICE_LINK_ERR', key: '62d29fab77bddd7',severity: 'ERROR', timestamp: 1542199783055, title: 'Link went down unexpectedly', }, },
                    severityCounts = Object.values(events).reduce((r, {severity}) => {
                    r[severity] = ( r[severity] || 0 ) + 1;
                    return r;
                    },{ CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0});
                    console.log(severityCounts);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 8:08









                    Hassan ImamHassan Imam

                    11.7k31230




                    11.7k31230























                        1














                        Please always post your attempt in OP before asking so that we get to know where you are stuck



                        You can loop through the object using for .. of and increment counter like below. No need for lodash






                        const events = {
                        '12327ae260fe9431': {
                        eventType: 'DEVICE_LINK_ERR',
                        key: '12327ae260fe9431',
                        severity: 'ERROR',
                        timestamp: 1542199782975,
                        title: 'Link went down unexpectedly',
                        },
                        '62d29fab77bddd7': {
                        eventType: 'DEVICE_LINK_ERR',
                        key: '62d29fab77bddd7',
                        severity: 'ERROR',
                        timestamp: 1542199783055,
                        title: 'Link went down unexpectedly',
                        },
                        '62d29fab77bddd71': {
                        eventType: 'DEVICE_LINK_ERR',
                        key: '62d29fab77bddd7',
                        severity: 'INFO',
                        timestamp: 1542199783055,
                        title: 'Link went down unexpectedly',
                        }
                        }

                        let severityCounts = { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 }

                        for(let { severity } of Object.values(events)) {
                        severityCounts[severity] = severityCounts[severity] || 0
                        severityCounts[severity]++
                        }

                        console.log(severityCounts)








                        share|improve this answer




























                          1














                          Please always post your attempt in OP before asking so that we get to know where you are stuck



                          You can loop through the object using for .. of and increment counter like below. No need for lodash






                          const events = {
                          '12327ae260fe9431': {
                          eventType: 'DEVICE_LINK_ERR',
                          key: '12327ae260fe9431',
                          severity: 'ERROR',
                          timestamp: 1542199782975,
                          title: 'Link went down unexpectedly',
                          },
                          '62d29fab77bddd7': {
                          eventType: 'DEVICE_LINK_ERR',
                          key: '62d29fab77bddd7',
                          severity: 'ERROR',
                          timestamp: 1542199783055,
                          title: 'Link went down unexpectedly',
                          },
                          '62d29fab77bddd71': {
                          eventType: 'DEVICE_LINK_ERR',
                          key: '62d29fab77bddd7',
                          severity: 'INFO',
                          timestamp: 1542199783055,
                          title: 'Link went down unexpectedly',
                          }
                          }

                          let severityCounts = { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 }

                          for(let { severity } of Object.values(events)) {
                          severityCounts[severity] = severityCounts[severity] || 0
                          severityCounts[severity]++
                          }

                          console.log(severityCounts)








                          share|improve this answer


























                            1












                            1








                            1







                            Please always post your attempt in OP before asking so that we get to know where you are stuck



                            You can loop through the object using for .. of and increment counter like below. No need for lodash






                            const events = {
                            '12327ae260fe9431': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '12327ae260fe9431',
                            severity: 'ERROR',
                            timestamp: 1542199782975,
                            title: 'Link went down unexpectedly',
                            },
                            '62d29fab77bddd7': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '62d29fab77bddd7',
                            severity: 'ERROR',
                            timestamp: 1542199783055,
                            title: 'Link went down unexpectedly',
                            },
                            '62d29fab77bddd71': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '62d29fab77bddd7',
                            severity: 'INFO',
                            timestamp: 1542199783055,
                            title: 'Link went down unexpectedly',
                            }
                            }

                            let severityCounts = { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 }

                            for(let { severity } of Object.values(events)) {
                            severityCounts[severity] = severityCounts[severity] || 0
                            severityCounts[severity]++
                            }

                            console.log(severityCounts)








                            share|improve this answer













                            Please always post your attempt in OP before asking so that we get to know where you are stuck



                            You can loop through the object using for .. of and increment counter like below. No need for lodash






                            const events = {
                            '12327ae260fe9431': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '12327ae260fe9431',
                            severity: 'ERROR',
                            timestamp: 1542199782975,
                            title: 'Link went down unexpectedly',
                            },
                            '62d29fab77bddd7': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '62d29fab77bddd7',
                            severity: 'ERROR',
                            timestamp: 1542199783055,
                            title: 'Link went down unexpectedly',
                            },
                            '62d29fab77bddd71': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '62d29fab77bddd7',
                            severity: 'INFO',
                            timestamp: 1542199783055,
                            title: 'Link went down unexpectedly',
                            }
                            }

                            let severityCounts = { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 }

                            for(let { severity } of Object.values(events)) {
                            severityCounts[severity] = severityCounts[severity] || 0
                            severityCounts[severity]++
                            }

                            console.log(severityCounts)








                            const events = {
                            '12327ae260fe9431': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '12327ae260fe9431',
                            severity: 'ERROR',
                            timestamp: 1542199782975,
                            title: 'Link went down unexpectedly',
                            },
                            '62d29fab77bddd7': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '62d29fab77bddd7',
                            severity: 'ERROR',
                            timestamp: 1542199783055,
                            title: 'Link went down unexpectedly',
                            },
                            '62d29fab77bddd71': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '62d29fab77bddd7',
                            severity: 'INFO',
                            timestamp: 1542199783055,
                            title: 'Link went down unexpectedly',
                            }
                            }

                            let severityCounts = { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 }

                            for(let { severity } of Object.values(events)) {
                            severityCounts[severity] = severityCounts[severity] || 0
                            severityCounts[severity]++
                            }

                            console.log(severityCounts)





                            const events = {
                            '12327ae260fe9431': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '12327ae260fe9431',
                            severity: 'ERROR',
                            timestamp: 1542199782975,
                            title: 'Link went down unexpectedly',
                            },
                            '62d29fab77bddd7': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '62d29fab77bddd7',
                            severity: 'ERROR',
                            timestamp: 1542199783055,
                            title: 'Link went down unexpectedly',
                            },
                            '62d29fab77bddd71': {
                            eventType: 'DEVICE_LINK_ERR',
                            key: '62d29fab77bddd7',
                            severity: 'INFO',
                            timestamp: 1542199783055,
                            title: 'Link went down unexpectedly',
                            }
                            }

                            let severityCounts = { CRITICAL: 0, ERROR: 0, INFO: 0, WARNING: 0 }

                            for(let { severity } of Object.values(events)) {
                            severityCounts[severity] = severityCounts[severity] || 0
                            severityCounts[severity]++
                            }

                            console.log(severityCounts)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 '18 at 8:01









                            Nitish NarangNitish Narang

                            2,938815




                            2,938815






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426210%2fhow-do-i-calculate-the-number-of-the-key-of-a-specific-property-from-a-json-obje%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                To store a contact into the json file from server.js file using a class in NodeJS

                                Redirect URL with Chrome Remote Debugging Android Devices

                                Dieringhausen