Group by data by key and return a new object containing the sum value of another key










0














I have data like that:



const data = [
name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
name: 'bob', colors: , count: 1, day: '2018-11-12',
name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
];


Now I want to group by data by day and get the sum of key count so obtain an array of objects like that:



const newData = [
day: '2018-11-12', countSum: 5, // sum of name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12', name: 'alice', colors: [blue'], count: 2, day: '2018-11-12', name: 'bob', colors: , count: 1, day: '2018-11-12', name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12'
day: '2018-12-12', countSum: 1,
day: '2018-11-11', countSum: 2, // sum of name: 'bob', colors: [blue'], count: 1, day: '2018-11-11', name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11'
day: '2018-11-10', countSum: 1,
day: '2018-11-01', countSum: 3,
day: '2017-11-12', countSum: 1,
]


I tried to use groupBy of Lodash to group data by day but I can't to count the sum of count key.
I need help.



Thanks a lot










share|improve this question




























    0














    I have data like that:



    const data = [
    name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
    name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
    name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
    name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
    name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
    name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
    name: 'bob', colors: , count: 1, day: '2018-11-12',
    name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
    name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
    name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
    ];


    Now I want to group by data by day and get the sum of key count so obtain an array of objects like that:



    const newData = [
    day: '2018-11-12', countSum: 5, // sum of name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12', name: 'alice', colors: [blue'], count: 2, day: '2018-11-12', name: 'bob', colors: , count: 1, day: '2018-11-12', name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12'
    day: '2018-12-12', countSum: 1,
    day: '2018-11-11', countSum: 2, // sum of name: 'bob', colors: [blue'], count: 1, day: '2018-11-11', name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11'
    day: '2018-11-10', countSum: 1,
    day: '2018-11-01', countSum: 3,
    day: '2017-11-12', countSum: 1,
    ]


    I tried to use groupBy of Lodash to group data by day but I can't to count the sum of count key.
    I need help.



    Thanks a lot










    share|improve this question


























      0












      0








      0







      I have data like that:



      const data = [
      name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
      name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
      name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
      name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
      name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
      name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
      name: 'bob', colors: , count: 1, day: '2018-11-12',
      name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
      name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
      name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
      ];


      Now I want to group by data by day and get the sum of key count so obtain an array of objects like that:



      const newData = [
      day: '2018-11-12', countSum: 5, // sum of name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12', name: 'alice', colors: [blue'], count: 2, day: '2018-11-12', name: 'bob', colors: , count: 1, day: '2018-11-12', name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12'
      day: '2018-12-12', countSum: 1,
      day: '2018-11-11', countSum: 2, // sum of name: 'bob', colors: [blue'], count: 1, day: '2018-11-11', name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11'
      day: '2018-11-10', countSum: 1,
      day: '2018-11-01', countSum: 3,
      day: '2017-11-12', countSum: 1,
      ]


      I tried to use groupBy of Lodash to group data by day but I can't to count the sum of count key.
      I need help.



      Thanks a lot










      share|improve this question















      I have data like that:



      const data = [
      name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
      name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
      name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
      name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
      name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
      name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
      name: 'bob', colors: , count: 1, day: '2018-11-12',
      name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
      name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
      name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
      ];


      Now I want to group by data by day and get the sum of key count so obtain an array of objects like that:



      const newData = [
      day: '2018-11-12', countSum: 5, // sum of name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12', name: 'alice', colors: [blue'], count: 2, day: '2018-11-12', name: 'bob', colors: , count: 1, day: '2018-11-12', name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12'
      day: '2018-12-12', countSum: 1,
      day: '2018-11-11', countSum: 2, // sum of name: 'bob', colors: [blue'], count: 1, day: '2018-11-11', name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11'
      day: '2018-11-10', countSum: 1,
      day: '2018-11-01', countSum: 3,
      day: '2017-11-12', countSum: 1,
      ]


      I tried to use groupBy of Lodash to group data by day but I can't to count the sum of count key.
      I need help.



      Thanks a lot







      javascript lodash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 12:25









      Peter Bode

      1358




      1358










      asked Nov 12 at 12:13









      Carl Rouge

      1198




      1198






















          3 Answers
          3






          active

          oldest

          votes


















          2














          Similar to "Reduce" solution by charlietfl, you can use for..of too.






          const data = [
          name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
          name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
          name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
          name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
          name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
          name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
          name: 'bob', colors: , count: 1, day: '2018-11-12',
          name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
          name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
          name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
          ];

          let result =

          for(let d of data)
          result[d.day] = result[d.day]

          console.log(Object.values(result))








          share|improve this answer




























            5














            You can use Array#reduce() to create an object with the common property value (day) as keys then use Object.values() to return expected array






            const counts = data.reduce((a, day, count) => 
            a[day] = a[day] ,)

            const res = Object.values(counts)

            console.log(res)

            <script>
            const data = [
            name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
            name: 'alice', colors: ['blue'], count: 2, day: '2018-11-12',
            name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
            name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
            name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
            name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
            name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
            name: 'bob', colors: , count: 1, day: '2018-11-12',
            name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
            name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
            name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
            ]
            </script>








            share|improve this answer






























              1














              Here is a lodash approach. First groupBy day and then map each result of that group, and perform sumBy on each groups by count, That's it






              var data = ["name":"alice","colors":["red","blue"],"count":1,"day":"2018-11-12","name":"duke","colors":["red","blue"],"count":1,"day":"2018-12-12","name":"bob","colors":["blue"],"count":1,"day":"2018-11-11","name":"alice","colors":["blue"],"count":1,"day":"2018-11-10","name":"carl","colors":["blue"],"count":3,"day":"2018-11-01","name":"bob","colors":["red"],"count":1,"day":"2017-11-12","name":"bob","colors":,"count":1,"day":"2018-11-12","name":"bob","colors":["red","blue","yellow"],"count":1,"day":"2018-11-11","name":"alice","colors":["yellow"],"count":2,"day":"2018-11-11","name":"duke","colors":["red","yellow"],"count":1,"day":"2018-11-12"];

              var res =_(data)
              .groupBy('day')
              .map((grp, day) => (day, countSum: _.sumBy(grp, 'count')))
              .value();

              console.log(res);

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








              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%2f53261979%2fgroup-by-data-by-key-and-return-a-new-object-containing-the-sum-value-of-another%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














                Similar to "Reduce" solution by charlietfl, you can use for..of too.






                const data = [
                name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                name: 'bob', colors: , count: 1, day: '2018-11-12',
                name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                ];

                let result =

                for(let d of data)
                result[d.day] = result[d.day]

                console.log(Object.values(result))








                share|improve this answer

























                  2














                  Similar to "Reduce" solution by charlietfl, you can use for..of too.






                  const data = [
                  name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                  name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                  name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                  name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                  name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                  name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                  name: 'bob', colors: , count: 1, day: '2018-11-12',
                  name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                  name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                  name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                  ];

                  let result =

                  for(let d of data)
                  result[d.day] = result[d.day]

                  console.log(Object.values(result))








                  share|improve this answer























                    2












                    2








                    2






                    Similar to "Reduce" solution by charlietfl, you can use for..of too.






                    const data = [
                    name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                    name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                    name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                    name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                    name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                    name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                    name: 'bob', colors: , count: 1, day: '2018-11-12',
                    name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                    name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                    name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                    ];

                    let result =

                    for(let d of data)
                    result[d.day] = result[d.day]

                    console.log(Object.values(result))








                    share|improve this answer












                    Similar to "Reduce" solution by charlietfl, you can use for..of too.






                    const data = [
                    name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                    name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                    name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                    name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                    name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                    name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                    name: 'bob', colors: , count: 1, day: '2018-11-12',
                    name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                    name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                    name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                    ];

                    let result =

                    for(let d of data)
                    result[d.day] = result[d.day]

                    console.log(Object.values(result))








                    const data = [
                    name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                    name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                    name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                    name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                    name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                    name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                    name: 'bob', colors: , count: 1, day: '2018-11-12',
                    name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                    name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                    name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                    ];

                    let result =

                    for(let d of data)
                    result[d.day] = result[d.day]

                    console.log(Object.values(result))





                    const data = [
                    name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                    name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                    name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                    name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                    name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                    name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                    name: 'bob', colors: , count: 1, day: '2018-11-12',
                    name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                    name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                    name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                    ];

                    let result =

                    for(let d of data)
                    result[d.day] = result[d.day]

                    console.log(Object.values(result))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 at 12:38









                    Nitish Narang

                    2,948815




                    2,948815























                        5














                        You can use Array#reduce() to create an object with the common property value (day) as keys then use Object.values() to return expected array






                        const counts = data.reduce((a, day, count) => 
                        a[day] = a[day] ,)

                        const res = Object.values(counts)

                        console.log(res)

                        <script>
                        const data = [
                        name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                        name: 'alice', colors: ['blue'], count: 2, day: '2018-11-12',
                        name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                        name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                        name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                        name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                        name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                        name: 'bob', colors: , count: 1, day: '2018-11-12',
                        name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                        name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                        name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                        ]
                        </script>








                        share|improve this answer



























                          5














                          You can use Array#reduce() to create an object with the common property value (day) as keys then use Object.values() to return expected array






                          const counts = data.reduce((a, day, count) => 
                          a[day] = a[day] ,)

                          const res = Object.values(counts)

                          console.log(res)

                          <script>
                          const data = [
                          name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                          name: 'alice', colors: ['blue'], count: 2, day: '2018-11-12',
                          name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                          name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                          name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                          name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                          name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                          name: 'bob', colors: , count: 1, day: '2018-11-12',
                          name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                          name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                          name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                          ]
                          </script>








                          share|improve this answer

























                            5












                            5








                            5






                            You can use Array#reduce() to create an object with the common property value (day) as keys then use Object.values() to return expected array






                            const counts = data.reduce((a, day, count) => 
                            a[day] = a[day] ,)

                            const res = Object.values(counts)

                            console.log(res)

                            <script>
                            const data = [
                            name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                            name: 'alice', colors: ['blue'], count: 2, day: '2018-11-12',
                            name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                            name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                            name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                            name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                            name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                            name: 'bob', colors: , count: 1, day: '2018-11-12',
                            name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                            name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                            name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                            ]
                            </script>








                            share|improve this answer














                            You can use Array#reduce() to create an object with the common property value (day) as keys then use Object.values() to return expected array






                            const counts = data.reduce((a, day, count) => 
                            a[day] = a[day] ,)

                            const res = Object.values(counts)

                            console.log(res)

                            <script>
                            const data = [
                            name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                            name: 'alice', colors: ['blue'], count: 2, day: '2018-11-12',
                            name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                            name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                            name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                            name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                            name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                            name: 'bob', colors: , count: 1, day: '2018-11-12',
                            name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                            name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                            name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                            ]
                            </script>








                            const counts = data.reduce((a, day, count) => 
                            a[day] = a[day] ,)

                            const res = Object.values(counts)

                            console.log(res)

                            <script>
                            const data = [
                            name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                            name: 'alice', colors: ['blue'], count: 2, day: '2018-11-12',
                            name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                            name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                            name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                            name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                            name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                            name: 'bob', colors: , count: 1, day: '2018-11-12',
                            name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                            name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                            name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                            ]
                            </script>





                            const counts = data.reduce((a, day, count) => 
                            a[day] = a[day] ,)

                            const res = Object.values(counts)

                            console.log(res)

                            <script>
                            const data = [
                            name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12',
                            name: 'alice', colors: ['blue'], count: 2, day: '2018-11-12',
                            name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12',
                            name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11',
                            name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10',
                            name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01',
                            name: 'bob', colors: ['red'], count: 1, day: '2017-11-12',
                            name: 'bob', colors: , count: 1, day: '2018-11-12',
                            name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11',
                            name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11',
                            name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12',
                            ]
                            </script>






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 12 at 12:34

























                            answered Nov 12 at 12:24









                            charlietfl

                            139k1386118




                            139k1386118





















                                1














                                Here is a lodash approach. First groupBy day and then map each result of that group, and perform sumBy on each groups by count, That's it






                                var data = ["name":"alice","colors":["red","blue"],"count":1,"day":"2018-11-12","name":"duke","colors":["red","blue"],"count":1,"day":"2018-12-12","name":"bob","colors":["blue"],"count":1,"day":"2018-11-11","name":"alice","colors":["blue"],"count":1,"day":"2018-11-10","name":"carl","colors":["blue"],"count":3,"day":"2018-11-01","name":"bob","colors":["red"],"count":1,"day":"2017-11-12","name":"bob","colors":,"count":1,"day":"2018-11-12","name":"bob","colors":["red","blue","yellow"],"count":1,"day":"2018-11-11","name":"alice","colors":["yellow"],"count":2,"day":"2018-11-11","name":"duke","colors":["red","yellow"],"count":1,"day":"2018-11-12"];

                                var res =_(data)
                                .groupBy('day')
                                .map((grp, day) => (day, countSum: _.sumBy(grp, 'count')))
                                .value();

                                console.log(res);

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








                                share|improve this answer



























                                  1














                                  Here is a lodash approach. First groupBy day and then map each result of that group, and perform sumBy on each groups by count, That's it






                                  var data = ["name":"alice","colors":["red","blue"],"count":1,"day":"2018-11-12","name":"duke","colors":["red","blue"],"count":1,"day":"2018-12-12","name":"bob","colors":["blue"],"count":1,"day":"2018-11-11","name":"alice","colors":["blue"],"count":1,"day":"2018-11-10","name":"carl","colors":["blue"],"count":3,"day":"2018-11-01","name":"bob","colors":["red"],"count":1,"day":"2017-11-12","name":"bob","colors":,"count":1,"day":"2018-11-12","name":"bob","colors":["red","blue","yellow"],"count":1,"day":"2018-11-11","name":"alice","colors":["yellow"],"count":2,"day":"2018-11-11","name":"duke","colors":["red","yellow"],"count":1,"day":"2018-11-12"];

                                  var res =_(data)
                                  .groupBy('day')
                                  .map((grp, day) => (day, countSum: _.sumBy(grp, 'count')))
                                  .value();

                                  console.log(res);

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








                                  share|improve this answer

























                                    1












                                    1








                                    1






                                    Here is a lodash approach. First groupBy day and then map each result of that group, and perform sumBy on each groups by count, That's it






                                    var data = ["name":"alice","colors":["red","blue"],"count":1,"day":"2018-11-12","name":"duke","colors":["red","blue"],"count":1,"day":"2018-12-12","name":"bob","colors":["blue"],"count":1,"day":"2018-11-11","name":"alice","colors":["blue"],"count":1,"day":"2018-11-10","name":"carl","colors":["blue"],"count":3,"day":"2018-11-01","name":"bob","colors":["red"],"count":1,"day":"2017-11-12","name":"bob","colors":,"count":1,"day":"2018-11-12","name":"bob","colors":["red","blue","yellow"],"count":1,"day":"2018-11-11","name":"alice","colors":["yellow"],"count":2,"day":"2018-11-11","name":"duke","colors":["red","yellow"],"count":1,"day":"2018-11-12"];

                                    var res =_(data)
                                    .groupBy('day')
                                    .map((grp, day) => (day, countSum: _.sumBy(grp, 'count')))
                                    .value();

                                    console.log(res);

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








                                    share|improve this answer














                                    Here is a lodash approach. First groupBy day and then map each result of that group, and perform sumBy on each groups by count, That's it






                                    var data = ["name":"alice","colors":["red","blue"],"count":1,"day":"2018-11-12","name":"duke","colors":["red","blue"],"count":1,"day":"2018-12-12","name":"bob","colors":["blue"],"count":1,"day":"2018-11-11","name":"alice","colors":["blue"],"count":1,"day":"2018-11-10","name":"carl","colors":["blue"],"count":3,"day":"2018-11-01","name":"bob","colors":["red"],"count":1,"day":"2017-11-12","name":"bob","colors":,"count":1,"day":"2018-11-12","name":"bob","colors":["red","blue","yellow"],"count":1,"day":"2018-11-11","name":"alice","colors":["yellow"],"count":2,"day":"2018-11-11","name":"duke","colors":["red","yellow"],"count":1,"day":"2018-11-12"];

                                    var res =_(data)
                                    .groupBy('day')
                                    .map((grp, day) => (day, countSum: _.sumBy(grp, 'count')))
                                    .value();

                                    console.log(res);

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








                                    var data = ["name":"alice","colors":["red","blue"],"count":1,"day":"2018-11-12","name":"duke","colors":["red","blue"],"count":1,"day":"2018-12-12","name":"bob","colors":["blue"],"count":1,"day":"2018-11-11","name":"alice","colors":["blue"],"count":1,"day":"2018-11-10","name":"carl","colors":["blue"],"count":3,"day":"2018-11-01","name":"bob","colors":["red"],"count":1,"day":"2017-11-12","name":"bob","colors":,"count":1,"day":"2018-11-12","name":"bob","colors":["red","blue","yellow"],"count":1,"day":"2018-11-11","name":"alice","colors":["yellow"],"count":2,"day":"2018-11-11","name":"duke","colors":["red","yellow"],"count":1,"day":"2018-11-12"];

                                    var res =_(data)
                                    .groupBy('day')
                                    .map((grp, day) => (day, countSum: _.sumBy(grp, 'count')))
                                    .value();

                                    console.log(res);

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





                                    var data = ["name":"alice","colors":["red","blue"],"count":1,"day":"2018-11-12","name":"duke","colors":["red","blue"],"count":1,"day":"2018-12-12","name":"bob","colors":["blue"],"count":1,"day":"2018-11-11","name":"alice","colors":["blue"],"count":1,"day":"2018-11-10","name":"carl","colors":["blue"],"count":3,"day":"2018-11-01","name":"bob","colors":["red"],"count":1,"day":"2017-11-12","name":"bob","colors":,"count":1,"day":"2018-11-12","name":"bob","colors":["red","blue","yellow"],"count":1,"day":"2018-11-11","name":"alice","colors":["yellow"],"count":2,"day":"2018-11-11","name":"duke","colors":["red","yellow"],"count":1,"day":"2018-11-12"];

                                    var res =_(data)
                                    .groupBy('day')
                                    .map((grp, day) => (day, countSum: _.sumBy(grp, 'count')))
                                    .value();

                                    console.log(res);

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






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 12 at 16:37

























                                    answered Nov 12 at 16:32









                                    Koushik Chatterjee

                                    2,73731024




                                    2,73731024



























                                        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.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • 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%2f53261979%2fgroup-by-data-by-key-and-return-a-new-object-containing-the-sum-value-of-another%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







                                        這個網誌中的熱門文章

                                        How to read a connectionString WITH PROVIDER in .NET Core?

                                        In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                                        Museum of Modern and Contemporary Art of Trento and Rovereto