JQuery - show only one if the same id and also add it on weeks [duplicate]











up vote
2
down vote

favorite
1













This question already has an answer here:




  • Most efficient method to groupby on a array of objects

    33 answers




I have this sample data:



[{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}]


I wanted that I will only show one if id is redundant but add all of the weeks that the id's had



My Attempt:



var data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
var weeks = 0;
for (var i=0; i<data.length; i++){
weeks += data[i].weeks;
}


Desired output



[{"id":"2", "weeks":6}, {"id":3, "weeks":7}]









share|improve this question













marked as duplicate by Andreas, Amit Joki javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 6:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    2
    down vote

    favorite
    1













    This question already has an answer here:




    • Most efficient method to groupby on a array of objects

      33 answers




    I have this sample data:



    [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}]


    I wanted that I will only show one if id is redundant but add all of the weeks that the id's had



    My Attempt:



    var data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
    var weeks = 0;
    for (var i=0; i<data.length; i++){
    weeks += data[i].weeks;
    }


    Desired output



    [{"id":"2", "weeks":6}, {"id":3, "weeks":7}]









    share|improve this question













    marked as duplicate by Andreas, Amit Joki javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 20 at 6:27


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1






      This question already has an answer here:




      • Most efficient method to groupby on a array of objects

        33 answers




      I have this sample data:



      [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}]


      I wanted that I will only show one if id is redundant but add all of the weeks that the id's had



      My Attempt:



      var data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
      var weeks = 0;
      for (var i=0; i<data.length; i++){
      weeks += data[i].weeks;
      }


      Desired output



      [{"id":"2", "weeks":6}, {"id":3, "weeks":7}]









      share|improve this question














      This question already has an answer here:




      • Most efficient method to groupby on a array of objects

        33 answers




      I have this sample data:



      [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}]


      I wanted that I will only show one if id is redundant but add all of the weeks that the id's had



      My Attempt:



      var data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
      var weeks = 0;
      for (var i=0; i<data.length; i++){
      weeks += data[i].weeks;
      }


      Desired output



      [{"id":"2", "weeks":6}, {"id":3, "weeks":7}]




      This question already has an answer here:




      • Most efficient method to groupby on a array of objects

        33 answers








      javascript jquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 6:24









      sad saddest

      338




      338




      marked as duplicate by Andreas, Amit Joki javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 20 at 6:27


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Andreas, Amit Joki javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 20 at 6:27


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You can use array#reduce() with Object.values(). Group your data based on id and add the weeks for repeating id in an object. Take the values from the object using Object.values().






          const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
          result = Object.values(data.reduce((r,{id,weeks}) => {
          r[id] = r[id] || {id, weeks : 0};
          r[id].weeks += weeks;
          return r;
          },{}));
          console.log(result);








          share|improve this answer




























            up vote
            1
            down vote













            You can Array.prototype.reduce() combined with Array.prototype.push() or Array.prototype.find() based on the number of repeated elements with same id



            Code:






            const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
            const result = data
            .reduce((a, c) => {
            a.temp[c.id] = ++a.temp[c.id] || 1;
            a.temp[c.id] === 1
            ? a.array.push(c)
            : a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
            return a;
            }, {temp: {}, array: })
            .array;

            console.log(result);








            share|improve this answer






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote



              accepted










              You can use array#reduce() with Object.values(). Group your data based on id and add the weeks for repeating id in an object. Take the values from the object using Object.values().






              const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
              result = Object.values(data.reduce((r,{id,weeks}) => {
              r[id] = r[id] || {id, weeks : 0};
              r[id].weeks += weeks;
              return r;
              },{}));
              console.log(result);








              share|improve this answer

























                up vote
                3
                down vote



                accepted










                You can use array#reduce() with Object.values(). Group your data based on id and add the weeks for repeating id in an object. Take the values from the object using Object.values().






                const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
                result = Object.values(data.reduce((r,{id,weeks}) => {
                r[id] = r[id] || {id, weeks : 0};
                r[id].weeks += weeks;
                return r;
                },{}));
                console.log(result);








                share|improve this answer























                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  You can use array#reduce() with Object.values(). Group your data based on id and add the weeks for repeating id in an object. Take the values from the object using Object.values().






                  const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
                  result = Object.values(data.reduce((r,{id,weeks}) => {
                  r[id] = r[id] || {id, weeks : 0};
                  r[id].weeks += weeks;
                  return r;
                  },{}));
                  console.log(result);








                  share|improve this answer












                  You can use array#reduce() with Object.values(). Group your data based on id and add the weeks for repeating id in an object. Take the values from the object using Object.values().






                  const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
                  result = Object.values(data.reduce((r,{id,weeks}) => {
                  r[id] = r[id] || {id, weeks : 0};
                  r[id].weeks += weeks;
                  return r;
                  },{}));
                  console.log(result);








                  const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
                  result = Object.values(data.reduce((r,{id,weeks}) => {
                  r[id] = r[id] || {id, weeks : 0};
                  r[id].weeks += weeks;
                  return r;
                  },{}));
                  console.log(result);





                  const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
                  result = Object.values(data.reduce((r,{id,weeks}) => {
                  r[id] = r[id] || {id, weeks : 0};
                  r[id].weeks += weeks;
                  return r;
                  },{}));
                  console.log(result);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 6:29









                  Hassan Imam

                  11.8k31230




                  11.8k31230
























                      up vote
                      1
                      down vote













                      You can Array.prototype.reduce() combined with Array.prototype.push() or Array.prototype.find() based on the number of repeated elements with same id



                      Code:






                      const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
                      const result = data
                      .reduce((a, c) => {
                      a.temp[c.id] = ++a.temp[c.id] || 1;
                      a.temp[c.id] === 1
                      ? a.array.push(c)
                      : a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
                      return a;
                      }, {temp: {}, array: })
                      .array;

                      console.log(result);








                      share|improve this answer



























                        up vote
                        1
                        down vote













                        You can Array.prototype.reduce() combined with Array.prototype.push() or Array.prototype.find() based on the number of repeated elements with same id



                        Code:






                        const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
                        const result = data
                        .reduce((a, c) => {
                        a.temp[c.id] = ++a.temp[c.id] || 1;
                        a.temp[c.id] === 1
                        ? a.array.push(c)
                        : a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
                        return a;
                        }, {temp: {}, array: })
                        .array;

                        console.log(result);








                        share|improve this answer

























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          You can Array.prototype.reduce() combined with Array.prototype.push() or Array.prototype.find() based on the number of repeated elements with same id



                          Code:






                          const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
                          const result = data
                          .reduce((a, c) => {
                          a.temp[c.id] = ++a.temp[c.id] || 1;
                          a.temp[c.id] === 1
                          ? a.array.push(c)
                          : a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
                          return a;
                          }, {temp: {}, array: })
                          .array;

                          console.log(result);








                          share|improve this answer














                          You can Array.prototype.reduce() combined with Array.prototype.push() or Array.prototype.find() based on the number of repeated elements with same id



                          Code:






                          const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
                          const result = data
                          .reduce((a, c) => {
                          a.temp[c.id] = ++a.temp[c.id] || 1;
                          a.temp[c.id] === 1
                          ? a.array.push(c)
                          : a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
                          return a;
                          }, {temp: {}, array: })
                          .array;

                          console.log(result);








                          const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
                          const result = data
                          .reduce((a, c) => {
                          a.temp[c.id] = ++a.temp[c.id] || 1;
                          a.temp[c.id] === 1
                          ? a.array.push(c)
                          : a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
                          return a;
                          }, {temp: {}, array: })
                          .array;

                          console.log(result);





                          const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
                          const result = data
                          .reduce((a, c) => {
                          a.temp[c.id] = ++a.temp[c.id] || 1;
                          a.temp[c.id] === 1
                          ? a.array.push(c)
                          : a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
                          return a;
                          }, {temp: {}, array: })
                          .array;

                          console.log(result);






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 20 at 7:11

























                          answered Nov 20 at 6:29









                          Yosvel Quintero

                          10.8k42229




                          10.8k42229















                              Popular posts from this blog

                              Create new schema in PostgreSQL using DBeaver

                              Deepest pit of an array with Javascript: test on Codility

                              Costa Masnaga