Array of objects manipulation












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


      1





      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 13 hours ago

























      asked 14 hours ago









      avatarZuko

      605




      605
























          3 Answers
          3






          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] || (acc[email] = {email}))[date] = amount;
            return acc;
            }, {})
            );

            /*
            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) || createEl(acc, email))[date] = amount;
            return acc;
            }, );

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

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








            share|improve this answer





























              0














              The issue is you are not storing all the email related values together, Since all the values related to a same email should be grouped together, that should be your key, think in terms of hashMap.
              Create a unique key using
              acc[email] = ( acc[email] || { email }); which ctually creates an object if it doesn't exist.



              Once you find that key, add that date as a value in it, along with amount.
              acc[email][date] = amount



              Added Enhanced code:






              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'},]

              const updatedResult = data.reduce((acc, {email, date, amount}) => {
              acc[email] = ( acc[email] || { email });
              acc[email][date] = amount
              return acc;
              }, {});

              console.log(updatedResult);
              console.log("####### As expected value #######");
              console.log(Object.value(updatedResult));








              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

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                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 14 hours ago









                    CertainPerformance

                    75.6k143761




                    75.6k143761

























                        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] || (acc[email] = {email}))[date] = amount;
                        return acc;
                        }, {})
                        );

                        /*
                        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) || createEl(acc, email))[date] = amount;
                        return 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] || (acc[email] = {email}))[date] = amount;
                          return acc;
                          }, {})
                          );

                          /*
                          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) || createEl(acc, email))[date] = amount;
                          return 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] || (acc[email] = {email}))[date] = amount;
                            return acc;
                            }, {})
                            );

                            /*
                            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) || createEl(acc, email))[date] = amount;
                            return 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] || (acc[email] = {email}))[date] = amount;
                            return acc;
                            }, {})
                            );

                            /*
                            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) || createEl(acc, email))[date] = amount;
                            return 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] || (acc[email] = {email}))[date] = amount;
                            return acc;
                            }, {})
                            );

                            /*
                            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) || createEl(acc, email))[date] = amount;
                            return 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] || (acc[email] = {email}))[date] = amount;
                            return acc;
                            }, {})
                            );

                            /*
                            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) || createEl(acc, email))[date] = amount;
                            return 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 8 hours ago









                            Drew Reese

                            35919




                            35919























                                0














                                The issue is you are not storing all the email related values together, Since all the values related to a same email should be grouped together, that should be your key, think in terms of hashMap.
                                Create a unique key using
                                acc[email] = ( acc[email] || { email }); which ctually creates an object if it doesn't exist.



                                Once you find that key, add that date as a value in it, along with amount.
                                acc[email][date] = amount



                                Added Enhanced code:






                                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'},]

                                const updatedResult = data.reduce((acc, {email, date, amount}) => {
                                acc[email] = ( acc[email] || { email });
                                acc[email][date] = amount
                                return acc;
                                }, {});

                                console.log(updatedResult);
                                console.log("####### As expected value #######");
                                console.log(Object.value(updatedResult));








                                share|improve this answer


























                                  0














                                  The issue is you are not storing all the email related values together, Since all the values related to a same email should be grouped together, that should be your key, think in terms of hashMap.
                                  Create a unique key using
                                  acc[email] = ( acc[email] || { email }); which ctually creates an object if it doesn't exist.



                                  Once you find that key, add that date as a value in it, along with amount.
                                  acc[email][date] = amount



                                  Added Enhanced code:






                                  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'},]

                                  const updatedResult = data.reduce((acc, {email, date, amount}) => {
                                  acc[email] = ( acc[email] || { email });
                                  acc[email][date] = amount
                                  return acc;
                                  }, {});

                                  console.log(updatedResult);
                                  console.log("####### As expected value #######");
                                  console.log(Object.value(updatedResult));








                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    The issue is you are not storing all the email related values together, Since all the values related to a same email should be grouped together, that should be your key, think in terms of hashMap.
                                    Create a unique key using
                                    acc[email] = ( acc[email] || { email }); which ctually creates an object if it doesn't exist.



                                    Once you find that key, add that date as a value in it, along with amount.
                                    acc[email][date] = amount



                                    Added Enhanced code:






                                    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'},]

                                    const updatedResult = data.reduce((acc, {email, date, amount}) => {
                                    acc[email] = ( acc[email] || { email });
                                    acc[email][date] = amount
                                    return acc;
                                    }, {});

                                    console.log(updatedResult);
                                    console.log("####### As expected value #######");
                                    console.log(Object.value(updatedResult));








                                    share|improve this answer












                                    The issue is you are not storing all the email related values together, Since all the values related to a same email should be grouped together, that should be your key, think in terms of hashMap.
                                    Create a unique key using
                                    acc[email] = ( acc[email] || { email }); which ctually creates an object if it doesn't exist.



                                    Once you find that key, add that date as a value in it, along with amount.
                                    acc[email][date] = amount



                                    Added Enhanced code:






                                    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'},]

                                    const updatedResult = data.reduce((acc, {email, date, amount}) => {
                                    acc[email] = ( acc[email] || { email });
                                    acc[email][date] = amount
                                    return acc;
                                    }, {});

                                    console.log(updatedResult);
                                    console.log("####### As expected value #######");
                                    console.log(Object.value(updatedResult));








                                    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'},]

                                    const updatedResult = data.reduce((acc, {email, date, amount}) => {
                                    acc[email] = ( acc[email] || { email });
                                    acc[email][date] = amount
                                    return acc;
                                    }, {});

                                    console.log(updatedResult);
                                    console.log("####### As expected value #######");
                                    console.log(Object.value(updatedResult));





                                    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'},]

                                    const updatedResult = data.reduce((acc, {email, date, amount}) => {
                                    acc[email] = ( acc[email] || { email });
                                    acc[email][date] = amount
                                    return acc;
                                    }, {});

                                    console.log(updatedResult);
                                    console.log("####### As expected value #######");
                                    console.log(Object.value(updatedResult));






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 14 hours ago









                                    Hiteshdua1

                                    1,053617




                                    1,053617






























                                        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%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

                                        Eastern Orthodox Church

                                        Zagreb

                                        Understanding the information contained in the Deep Space Network XML data?