Array of objects manipulation

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












8















I feel that im close to the answer but I am not outputting exactly the format Im looking for



So, I have this array of objects:



const data = [
email: '100@email.com', amount: '30', date: '2018-12',
email: '100@email.com', amount: '30', date: '2018-11',
email: '100@email.com', amount: '30', date: '2018-10',

email: '200@email.com', amount: 0, date: '2018-12',
email: '200@email.com', amount:'30', date: '2018-11',
email: '200@email.com', amount:'30', date: '2018-10',
email: '200@email.com', amount:'30', date: '2018-09',
email: '200@email.com', amount:'25', date: '2018-08',
email: '200@email.com', amount:'25', date: '2018-08',]


As you can see in the data set there are repeated emails as well as duplicate objects like the last 2 in the data set.



I want to turn it into this array of objects:



const data = [

email: '100@email.com',
'2018-12': '30',
'2018-11': '30',
'2018-10': '30',
'2018-09': 0,
'2018-08': 0,
'2018-07': 0,
'2018-06': 0,
'2018-05': 0,
'2018-04': 0,
'2018-03': 0,
'2018-02': 0,
'2018-01': 0,
'2017-12': 0,
,

email: '200@email.com',
'2018-12':0,
'2018-11':'30',
'2018-10':'30',
'2018-09':'30',
'2018-08':'25',
'2018-07': 0,
'2018-06': 0,
'2018-05': 0,
'2018-04': 0,
'2018-03': 0,
'2018-02': 0,
'2018-01': 0,
'2017-12': 0,
]


The output has a range of dates from 2017-12 to 2018-12 and the value for the date key is the amount for that specific date, else if the date is not found on the object the value for that date defaults to 0



At the moment Im playing with the reduce() function using something like this:



let result = data.reduce((acc, email, date, amount) => 
acc.email = email
acc[date] = amount
return acc;
, );


result is only returning the last email with not exactly the range of dates Im looking for.



Thanks in advance for your help.










share|improve this question




























    8















    I feel that im close to the answer but I am not outputting exactly the format Im looking for



    So, I have this array of objects:



    const data = [
    email: '100@email.com', amount: '30', date: '2018-12',
    email: '100@email.com', amount: '30', date: '2018-11',
    email: '100@email.com', amount: '30', date: '2018-10',

    email: '200@email.com', amount: 0, date: '2018-12',
    email: '200@email.com', amount:'30', date: '2018-11',
    email: '200@email.com', amount:'30', date: '2018-10',
    email: '200@email.com', amount:'30', date: '2018-09',
    email: '200@email.com', amount:'25', date: '2018-08',
    email: '200@email.com', amount:'25', date: '2018-08',]


    As you can see in the data set there are repeated emails as well as duplicate objects like the last 2 in the data set.



    I want to turn it into this array of objects:



    const data = [

    email: '100@email.com',
    '2018-12': '30',
    '2018-11': '30',
    '2018-10': '30',
    '2018-09': 0,
    '2018-08': 0,
    '2018-07': 0,
    '2018-06': 0,
    '2018-05': 0,
    '2018-04': 0,
    '2018-03': 0,
    '2018-02': 0,
    '2018-01': 0,
    '2017-12': 0,
    ,

    email: '200@email.com',
    '2018-12':0,
    '2018-11':'30',
    '2018-10':'30',
    '2018-09':'30',
    '2018-08':'25',
    '2018-07': 0,
    '2018-06': 0,
    '2018-05': 0,
    '2018-04': 0,
    '2018-03': 0,
    '2018-02': 0,
    '2018-01': 0,
    '2017-12': 0,
    ]


    The output has a range of dates from 2017-12 to 2018-12 and the value for the date key is the amount for that specific date, else if the date is not found on the object the value for that date defaults to 0



    At the moment Im playing with the reduce() function using something like this:



    let result = data.reduce((acc, email, date, amount) => 
    acc.email = email
    acc[date] = amount
    return acc;
    , );


    result is only returning the last email with not exactly the range of dates Im looking for.



    Thanks in advance for your help.










    share|improve this question


























      8












      8








      8


      2






      I feel that im close to the answer but I am not outputting exactly the format Im looking for



      So, I have this array of objects:



      const data = [
      email: '100@email.com', amount: '30', date: '2018-12',
      email: '100@email.com', amount: '30', date: '2018-11',
      email: '100@email.com', amount: '30', date: '2018-10',

      email: '200@email.com', amount: 0, date: '2018-12',
      email: '200@email.com', amount:'30', date: '2018-11',
      email: '200@email.com', amount:'30', date: '2018-10',
      email: '200@email.com', amount:'30', date: '2018-09',
      email: '200@email.com', amount:'25', date: '2018-08',
      email: '200@email.com', amount:'25', date: '2018-08',]


      As you can see in the data set there are repeated emails as well as duplicate objects like the last 2 in the data set.



      I want to turn it into this array of objects:



      const data = [

      email: '100@email.com',
      '2018-12': '30',
      '2018-11': '30',
      '2018-10': '30',
      '2018-09': 0,
      '2018-08': 0,
      '2018-07': 0,
      '2018-06': 0,
      '2018-05': 0,
      '2018-04': 0,
      '2018-03': 0,
      '2018-02': 0,
      '2018-01': 0,
      '2017-12': 0,
      ,

      email: '200@email.com',
      '2018-12':0,
      '2018-11':'30',
      '2018-10':'30',
      '2018-09':'30',
      '2018-08':'25',
      '2018-07': 0,
      '2018-06': 0,
      '2018-05': 0,
      '2018-04': 0,
      '2018-03': 0,
      '2018-02': 0,
      '2018-01': 0,
      '2017-12': 0,
      ]


      The output has a range of dates from 2017-12 to 2018-12 and the value for the date key is the amount for that specific date, else if the date is not found on the object the value for that date defaults to 0



      At the moment Im playing with the reduce() function using something like this:



      let result = data.reduce((acc, email, date, amount) => 
      acc.email = email
      acc[date] = amount
      return acc;
      , );


      result is only returning the last email with not exactly the range of dates Im looking for.



      Thanks in advance for your help.










      share|improve this question
















      I feel that im close to the answer but I am not outputting exactly the format Im looking for



      So, I have this array of objects:



      const data = [
      email: '100@email.com', amount: '30', date: '2018-12',
      email: '100@email.com', amount: '30', date: '2018-11',
      email: '100@email.com', amount: '30', date: '2018-10',

      email: '200@email.com', amount: 0, date: '2018-12',
      email: '200@email.com', amount:'30', date: '2018-11',
      email: '200@email.com', amount:'30', date: '2018-10',
      email: '200@email.com', amount:'30', date: '2018-09',
      email: '200@email.com', amount:'25', date: '2018-08',
      email: '200@email.com', amount:'25', date: '2018-08',]


      As you can see in the data set there are repeated emails as well as duplicate objects like the last 2 in the data set.



      I want to turn it into this array of objects:



      const data = [

      email: '100@email.com',
      '2018-12': '30',
      '2018-11': '30',
      '2018-10': '30',
      '2018-09': 0,
      '2018-08': 0,
      '2018-07': 0,
      '2018-06': 0,
      '2018-05': 0,
      '2018-04': 0,
      '2018-03': 0,
      '2018-02': 0,
      '2018-01': 0,
      '2017-12': 0,
      ,

      email: '200@email.com',
      '2018-12':0,
      '2018-11':'30',
      '2018-10':'30',
      '2018-09':'30',
      '2018-08':'25',
      '2018-07': 0,
      '2018-06': 0,
      '2018-05': 0,
      '2018-04': 0,
      '2018-03': 0,
      '2018-02': 0,
      '2018-01': 0,
      '2017-12': 0,
      ]


      The output has a range of dates from 2017-12 to 2018-12 and the value for the date key is the amount for that specific date, else if the date is not found on the object the value for that date defaults to 0



      At the moment Im playing with the reduce() function using something like this:



      let result = data.reduce((acc, email, date, amount) => 
      acc.email = email
      acc[date] = amount
      return acc;
      , );


      result is only returning the last email with not exactly the range of dates Im looking for.



      Thanks in advance for your help.







      javascript node8.4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 31 '18 at 1:56







      avatarZuko

















      asked Dec 31 '18 at 0:46









      avatarZukoavatarZuko

      605




      605






















          2 Answers
          2






          active

          oldest

          votes


















          10














          Reduce into an object indexed by each email, creating the inner object explicitly if the [email] property doesn't exist on the accumulator yet. Once you're sure the object exists, you can assign to acc[email][date], and at the end, use Object.values to transform the object back into the desired array format:






          const data = [
          email: '100@email.com', amount: '30', date: '2018-12',
          email: '100@email.com', amount: '30', date: '2018-11',
          email: '100@email.com', amount: '30', date: '2018-10',

          email: '200@email.com', amount: 0, date: '2018-12',
          email: '200@email.com', amount:'30', date: '2018-11',
          email: '200@email.com', amount:'30', date: '2018-10',
          email: '200@email.com', amount:'30', date: '2018-09',
          email: '200@email.com', amount:'25', date: '2018-08',
          email: '200@email.com', amount:'25', date: '2018-08',]

          let result = data.reduce((acc, email, date, amount) =>
          if (!acc[email]) acc[email] = email ;
          acc[email][date] = amount;
          return acc;
          , );
          console.log(Object.values(result));








          share|improve this answer






























            1

















            const data = [
            email: '100@email.com', amount: '30', date: '2018-12',
            email: '100@email.com', amount: '30', date: '2018-11',
            email: '100@email.com', amount: '30', date: '2018-10',
            email: '200@email.com', amount: 0, date: '2018-12',
            email: '200@email.com', amount:'30', date: '2018-11',
            email: '200@email.com', amount:'30', date: '2018-10',
            email: '200@email.com', amount:'30', date: '2018-09',
            email: '200@email.com', amount:'25', date: '2018-08',
            email: '200@email.com', amount:'25', date: '2018-08',
            ];

            /*
            Reduce into an object with email as key and email
            and date/amount(s) as value, wrapped in Object.values
            to return the array of augmented data values.
            */
            augmentData = (data) => Object.values(
            data.reduce((acc, email, amount, date) =>
            (acc[email] , )
            );

            /*
            Reduce straight to array of augmented data
            objects containing email and date/amount values.
            */
            augmentData2 = data => data.reduce((acc, email, amount, date) =>
            const findEl = (arr) => arr.find(el => el.email == email);
            const createEl = (arr, email) => arr.push(email) && findEl(arr);

            (findEl(acc) , );

            const augmentedData = augmentData(data);
            console.log('augmentedData', augmentedData);

            const augmentedData2 = augmentData2(data);
            console.log('augmentedData2', augmentedData2);








            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%2f53982636%2farray-of-objects-manipulation%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              10














              Reduce into an object indexed by each email, creating the inner object explicitly if the [email] property doesn't exist on the accumulator yet. Once you're sure the object exists, you can assign to acc[email][date], and at the end, use Object.values to transform the object back into the desired array format:






              const data = [
              email: '100@email.com', amount: '30', date: '2018-12',
              email: '100@email.com', amount: '30', date: '2018-11',
              email: '100@email.com', amount: '30', date: '2018-10',

              email: '200@email.com', amount: 0, date: '2018-12',
              email: '200@email.com', amount:'30', date: '2018-11',
              email: '200@email.com', amount:'30', date: '2018-10',
              email: '200@email.com', amount:'30', date: '2018-09',
              email: '200@email.com', amount:'25', date: '2018-08',
              email: '200@email.com', amount:'25', date: '2018-08',]

              let result = data.reduce((acc, email, date, amount) =>
              if (!acc[email]) acc[email] = email ;
              acc[email][date] = amount;
              return acc;
              , );
              console.log(Object.values(result));








              share|improve this answer



























                10














                Reduce into an object indexed by each email, creating the inner object explicitly if the [email] property doesn't exist on the accumulator yet. Once you're sure the object exists, you can assign to acc[email][date], and at the end, use Object.values to transform the object back into the desired array format:






                const data = [
                email: '100@email.com', amount: '30', date: '2018-12',
                email: '100@email.com', amount: '30', date: '2018-11',
                email: '100@email.com', amount: '30', date: '2018-10',

                email: '200@email.com', amount: 0, date: '2018-12',
                email: '200@email.com', amount:'30', date: '2018-11',
                email: '200@email.com', amount:'30', date: '2018-10',
                email: '200@email.com', amount:'30', date: '2018-09',
                email: '200@email.com', amount:'25', date: '2018-08',
                email: '200@email.com', amount:'25', date: '2018-08',]

                let result = data.reduce((acc, email, date, amount) =>
                if (!acc[email]) acc[email] = email ;
                acc[email][date] = amount;
                return acc;
                , );
                console.log(Object.values(result));








                share|improve this answer

























                  10












                  10








                  10







                  Reduce into an object indexed by each email, creating the inner object explicitly if the [email] property doesn't exist on the accumulator yet. Once you're sure the object exists, you can assign to acc[email][date], and at the end, use Object.values to transform the object back into the desired array format:






                  const data = [
                  email: '100@email.com', amount: '30', date: '2018-12',
                  email: '100@email.com', amount: '30', date: '2018-11',
                  email: '100@email.com', amount: '30', date: '2018-10',

                  email: '200@email.com', amount: 0, date: '2018-12',
                  email: '200@email.com', amount:'30', date: '2018-11',
                  email: '200@email.com', amount:'30', date: '2018-10',
                  email: '200@email.com', amount:'30', date: '2018-09',
                  email: '200@email.com', amount:'25', date: '2018-08',
                  email: '200@email.com', amount:'25', date: '2018-08',]

                  let result = data.reduce((acc, email, date, amount) =>
                  if (!acc[email]) acc[email] = email ;
                  acc[email][date] = amount;
                  return acc;
                  , );
                  console.log(Object.values(result));








                  share|improve this answer













                  Reduce into an object indexed by each email, creating the inner object explicitly if the [email] property doesn't exist on the accumulator yet. Once you're sure the object exists, you can assign to acc[email][date], and at the end, use Object.values to transform the object back into the desired array format:






                  const data = [
                  email: '100@email.com', amount: '30', date: '2018-12',
                  email: '100@email.com', amount: '30', date: '2018-11',
                  email: '100@email.com', amount: '30', date: '2018-10',

                  email: '200@email.com', amount: 0, date: '2018-12',
                  email: '200@email.com', amount:'30', date: '2018-11',
                  email: '200@email.com', amount:'30', date: '2018-10',
                  email: '200@email.com', amount:'30', date: '2018-09',
                  email: '200@email.com', amount:'25', date: '2018-08',
                  email: '200@email.com', amount:'25', date: '2018-08',]

                  let result = data.reduce((acc, email, date, amount) =>
                  if (!acc[email]) acc[email] = email ;
                  acc[email][date] = amount;
                  return acc;
                  , );
                  console.log(Object.values(result));








                  const data = [
                  email: '100@email.com', amount: '30', date: '2018-12',
                  email: '100@email.com', amount: '30', date: '2018-11',
                  email: '100@email.com', amount: '30', date: '2018-10',

                  email: '200@email.com', amount: 0, date: '2018-12',
                  email: '200@email.com', amount:'30', date: '2018-11',
                  email: '200@email.com', amount:'30', date: '2018-10',
                  email: '200@email.com', amount:'30', date: '2018-09',
                  email: '200@email.com', amount:'25', date: '2018-08',
                  email: '200@email.com', amount:'25', date: '2018-08',]

                  let result = data.reduce((acc, email, date, amount) =>
                  if (!acc[email]) acc[email] = email ;
                  acc[email][date] = amount;
                  return acc;
                  , );
                  console.log(Object.values(result));





                  const data = [
                  email: '100@email.com', amount: '30', date: '2018-12',
                  email: '100@email.com', amount: '30', date: '2018-11',
                  email: '100@email.com', amount: '30', date: '2018-10',

                  email: '200@email.com', amount: 0, date: '2018-12',
                  email: '200@email.com', amount:'30', date: '2018-11',
                  email: '200@email.com', amount:'30', date: '2018-10',
                  email: '200@email.com', amount:'30', date: '2018-09',
                  email: '200@email.com', amount:'25', date: '2018-08',
                  email: '200@email.com', amount:'25', date: '2018-08',]

                  let result = data.reduce((acc, email, date, amount) =>
                  if (!acc[email]) acc[email] = email ;
                  acc[email][date] = amount;
                  return acc;
                  , );
                  console.log(Object.values(result));






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '18 at 0:50









                  CertainPerformanceCertainPerformance

                  79k143865




                  79k143865























                      1

















                      const data = [
                      email: '100@email.com', amount: '30', date: '2018-12',
                      email: '100@email.com', amount: '30', date: '2018-11',
                      email: '100@email.com', amount: '30', date: '2018-10',
                      email: '200@email.com', amount: 0, date: '2018-12',
                      email: '200@email.com', amount:'30', date: '2018-11',
                      email: '200@email.com', amount:'30', date: '2018-10',
                      email: '200@email.com', amount:'30', date: '2018-09',
                      email: '200@email.com', amount:'25', date: '2018-08',
                      email: '200@email.com', amount:'25', date: '2018-08',
                      ];

                      /*
                      Reduce into an object with email as key and email
                      and date/amount(s) as value, wrapped in Object.values
                      to return the array of augmented data values.
                      */
                      augmentData = (data) => Object.values(
                      data.reduce((acc, email, amount, date) =>
                      (acc[email] , )
                      );

                      /*
                      Reduce straight to array of augmented data
                      objects containing email and date/amount values.
                      */
                      augmentData2 = data => data.reduce((acc, email, amount, date) =>
                      const findEl = (arr) => arr.find(el => el.email == email);
                      const createEl = (arr, email) => arr.push(email) && findEl(arr);

                      (findEl(acc) , );

                      const augmentedData = augmentData(data);
                      console.log('augmentedData', augmentedData);

                      const augmentedData2 = augmentData2(data);
                      console.log('augmentedData2', augmentedData2);








                      share|improve this answer



























                        1

















                        const data = [
                        email: '100@email.com', amount: '30', date: '2018-12',
                        email: '100@email.com', amount: '30', date: '2018-11',
                        email: '100@email.com', amount: '30', date: '2018-10',
                        email: '200@email.com', amount: 0, date: '2018-12',
                        email: '200@email.com', amount:'30', date: '2018-11',
                        email: '200@email.com', amount:'30', date: '2018-10',
                        email: '200@email.com', amount:'30', date: '2018-09',
                        email: '200@email.com', amount:'25', date: '2018-08',
                        email: '200@email.com', amount:'25', date: '2018-08',
                        ];

                        /*
                        Reduce into an object with email as key and email
                        and date/amount(s) as value, wrapped in Object.values
                        to return the array of augmented data values.
                        */
                        augmentData = (data) => Object.values(
                        data.reduce((acc, email, amount, date) =>
                        (acc[email] , )
                        );

                        /*
                        Reduce straight to array of augmented data
                        objects containing email and date/amount values.
                        */
                        augmentData2 = data => data.reduce((acc, email, amount, date) =>
                        const findEl = (arr) => arr.find(el => el.email == email);
                        const createEl = (arr, email) => arr.push(email) && findEl(arr);

                        (findEl(acc) , );

                        const augmentedData = augmentData(data);
                        console.log('augmentedData', augmentedData);

                        const augmentedData2 = augmentData2(data);
                        console.log('augmentedData2', augmentedData2);








                        share|improve this answer

























                          1












                          1








                          1










                          const data = [
                          email: '100@email.com', amount: '30', date: '2018-12',
                          email: '100@email.com', amount: '30', date: '2018-11',
                          email: '100@email.com', amount: '30', date: '2018-10',
                          email: '200@email.com', amount: 0, date: '2018-12',
                          email: '200@email.com', amount:'30', date: '2018-11',
                          email: '200@email.com', amount:'30', date: '2018-10',
                          email: '200@email.com', amount:'30', date: '2018-09',
                          email: '200@email.com', amount:'25', date: '2018-08',
                          email: '200@email.com', amount:'25', date: '2018-08',
                          ];

                          /*
                          Reduce into an object with email as key and email
                          and date/amount(s) as value, wrapped in Object.values
                          to return the array of augmented data values.
                          */
                          augmentData = (data) => Object.values(
                          data.reduce((acc, email, amount, date) =>
                          (acc[email] , )
                          );

                          /*
                          Reduce straight to array of augmented data
                          objects containing email and date/amount values.
                          */
                          augmentData2 = data => data.reduce((acc, email, amount, date) =>
                          const findEl = (arr) => arr.find(el => el.email == email);
                          const createEl = (arr, email) => arr.push(email) && findEl(arr);

                          (findEl(acc) , );

                          const augmentedData = augmentData(data);
                          console.log('augmentedData', augmentedData);

                          const augmentedData2 = augmentData2(data);
                          console.log('augmentedData2', augmentedData2);








                          share|improve this answer
















                          const data = [
                          email: '100@email.com', amount: '30', date: '2018-12',
                          email: '100@email.com', amount: '30', date: '2018-11',
                          email: '100@email.com', amount: '30', date: '2018-10',
                          email: '200@email.com', amount: 0, date: '2018-12',
                          email: '200@email.com', amount:'30', date: '2018-11',
                          email: '200@email.com', amount:'30', date: '2018-10',
                          email: '200@email.com', amount:'30', date: '2018-09',
                          email: '200@email.com', amount:'25', date: '2018-08',
                          email: '200@email.com', amount:'25', date: '2018-08',
                          ];

                          /*
                          Reduce into an object with email as key and email
                          and date/amount(s) as value, wrapped in Object.values
                          to return the array of augmented data values.
                          */
                          augmentData = (data) => Object.values(
                          data.reduce((acc, email, amount, date) =>
                          (acc[email] , )
                          );

                          /*
                          Reduce straight to array of augmented data
                          objects containing email and date/amount values.
                          */
                          augmentData2 = data => data.reduce((acc, email, amount, date) =>
                          const findEl = (arr) => arr.find(el => el.email == email);
                          const createEl = (arr, email) => arr.push(email) && findEl(arr);

                          (findEl(acc) , );

                          const augmentedData = augmentData(data);
                          console.log('augmentedData', augmentedData);

                          const augmentedData2 = augmentData2(data);
                          console.log('augmentedData2', augmentedData2);








                          const data = [
                          email: '100@email.com', amount: '30', date: '2018-12',
                          email: '100@email.com', amount: '30', date: '2018-11',
                          email: '100@email.com', amount: '30', date: '2018-10',
                          email: '200@email.com', amount: 0, date: '2018-12',
                          email: '200@email.com', amount:'30', date: '2018-11',
                          email: '200@email.com', amount:'30', date: '2018-10',
                          email: '200@email.com', amount:'30', date: '2018-09',
                          email: '200@email.com', amount:'25', date: '2018-08',
                          email: '200@email.com', amount:'25', date: '2018-08',
                          ];

                          /*
                          Reduce into an object with email as key and email
                          and date/amount(s) as value, wrapped in Object.values
                          to return the array of augmented data values.
                          */
                          augmentData = (data) => Object.values(
                          data.reduce((acc, email, amount, date) =>
                          (acc[email] , )
                          );

                          /*
                          Reduce straight to array of augmented data
                          objects containing email and date/amount values.
                          */
                          augmentData2 = data => data.reduce((acc, email, amount, date) =>
                          const findEl = (arr) => arr.find(el => el.email == email);
                          const createEl = (arr, email) => arr.push(email) && findEl(arr);

                          (findEl(acc) , );

                          const augmentedData = augmentData(data);
                          console.log('augmentedData', augmentedData);

                          const augmentedData2 = augmentData2(data);
                          console.log('augmentedData2', augmentedData2);





                          const data = [
                          email: '100@email.com', amount: '30', date: '2018-12',
                          email: '100@email.com', amount: '30', date: '2018-11',
                          email: '100@email.com', amount: '30', date: '2018-10',
                          email: '200@email.com', amount: 0, date: '2018-12',
                          email: '200@email.com', amount:'30', date: '2018-11',
                          email: '200@email.com', amount:'30', date: '2018-10',
                          email: '200@email.com', amount:'30', date: '2018-09',
                          email: '200@email.com', amount:'25', date: '2018-08',
                          email: '200@email.com', amount:'25', date: '2018-08',
                          ];

                          /*
                          Reduce into an object with email as key and email
                          and date/amount(s) as value, wrapped in Object.values
                          to return the array of augmented data values.
                          */
                          augmentData = (data) => Object.values(
                          data.reduce((acc, email, amount, date) =>
                          (acc[email] , )
                          );

                          /*
                          Reduce straight to array of augmented data
                          objects containing email and date/amount values.
                          */
                          augmentData2 = data => data.reduce((acc, email, amount, date) =>
                          const findEl = (arr) => arr.find(el => el.email == email);
                          const createEl = (arr, email) => arr.push(email) && findEl(arr);

                          (findEl(acc) , );

                          const augmentedData = augmentData(data);
                          console.log('augmentedData', augmentedData);

                          const augmentedData2 = augmentData2(data);
                          console.log('augmentedData2', augmentedData2);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 31 '18 at 6:33









                          Drew ReeseDrew Reese

                          753211




                          753211



























                              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%2f53982636%2farray-of-objects-manipulation%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

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay