Reduce() an object of an element that has already been reduced












4















I was doing some data manipulation, but I made a mistake and got the wrong data structure, so initially my data is: (link to data)



const employees = [{
"EmployeeID": "100A",
"FirstName": "Downs",
"aval": [
{"start": "11-19", "end": "2", "ava": "30", "health": "4"},
{"start": "11-20", "end": "2", "ava": "40", "health": "4"},
{"start": "11-21", "end": "2", "ava": "50", "health": "4"},
{"start": "11-22", "end": "2", "ava": "60", "health": "4"}
]
},
{
"EmployeeID": "100B",
"FirstName": "Mckenzie",
"aval": [
{"start": "11-19", "end": "2", "ava": "1", "health": "4"},
{"start": "11-20", "end": "2", "ava": "2", "health": "4"},
{"start": "11-21", "end": "2", "ava": "3", "health": "4"},
{"start": "11-22", "end": "2", "ava": "4", "health": "4"}
]
},

]


and would like to get it like:



const employees = [
{ "EmployeeID": "100A", "11-19": "30", "11-20": "40", "11-21": "50", "11-22": "60"},
{ "EmployeeID": "100B", "11-19": "1", "11-20": "2", "11-21": "3", "11-22": "4"}
]


I've asked around and this gives me 10 objects instead of two, just need to put those pesky employeeID inside 1 object with all the start values altogether.



const res = employees.reduce((acc, { EmployeeID, aval}) => [
...acc,
...aval.map( ({ start, ava}) => ({ EmployeeID, [start]: ava}) )
], );









share|improve this question



























    4















    I was doing some data manipulation, but I made a mistake and got the wrong data structure, so initially my data is: (link to data)



    const employees = [{
    "EmployeeID": "100A",
    "FirstName": "Downs",
    "aval": [
    {"start": "11-19", "end": "2", "ava": "30", "health": "4"},
    {"start": "11-20", "end": "2", "ava": "40", "health": "4"},
    {"start": "11-21", "end": "2", "ava": "50", "health": "4"},
    {"start": "11-22", "end": "2", "ava": "60", "health": "4"}
    ]
    },
    {
    "EmployeeID": "100B",
    "FirstName": "Mckenzie",
    "aval": [
    {"start": "11-19", "end": "2", "ava": "1", "health": "4"},
    {"start": "11-20", "end": "2", "ava": "2", "health": "4"},
    {"start": "11-21", "end": "2", "ava": "3", "health": "4"},
    {"start": "11-22", "end": "2", "ava": "4", "health": "4"}
    ]
    },

    ]


    and would like to get it like:



    const employees = [
    { "EmployeeID": "100A", "11-19": "30", "11-20": "40", "11-21": "50", "11-22": "60"},
    { "EmployeeID": "100B", "11-19": "1", "11-20": "2", "11-21": "3", "11-22": "4"}
    ]


    I've asked around and this gives me 10 objects instead of two, just need to put those pesky employeeID inside 1 object with all the start values altogether.



    const res = employees.reduce((acc, { EmployeeID, aval}) => [
    ...acc,
    ...aval.map( ({ start, ava}) => ({ EmployeeID, [start]: ava}) )
    ], );









    share|improve this question

























      4












      4








      4








      I was doing some data manipulation, but I made a mistake and got the wrong data structure, so initially my data is: (link to data)



      const employees = [{
      "EmployeeID": "100A",
      "FirstName": "Downs",
      "aval": [
      {"start": "11-19", "end": "2", "ava": "30", "health": "4"},
      {"start": "11-20", "end": "2", "ava": "40", "health": "4"},
      {"start": "11-21", "end": "2", "ava": "50", "health": "4"},
      {"start": "11-22", "end": "2", "ava": "60", "health": "4"}
      ]
      },
      {
      "EmployeeID": "100B",
      "FirstName": "Mckenzie",
      "aval": [
      {"start": "11-19", "end": "2", "ava": "1", "health": "4"},
      {"start": "11-20", "end": "2", "ava": "2", "health": "4"},
      {"start": "11-21", "end": "2", "ava": "3", "health": "4"},
      {"start": "11-22", "end": "2", "ava": "4", "health": "4"}
      ]
      },

      ]


      and would like to get it like:



      const employees = [
      { "EmployeeID": "100A", "11-19": "30", "11-20": "40", "11-21": "50", "11-22": "60"},
      { "EmployeeID": "100B", "11-19": "1", "11-20": "2", "11-21": "3", "11-22": "4"}
      ]


      I've asked around and this gives me 10 objects instead of two, just need to put those pesky employeeID inside 1 object with all the start values altogether.



      const res = employees.reduce((acc, { EmployeeID, aval}) => [
      ...acc,
      ...aval.map( ({ start, ava}) => ({ EmployeeID, [start]: ava}) )
      ], );









      share|improve this question














      I was doing some data manipulation, but I made a mistake and got the wrong data structure, so initially my data is: (link to data)



      const employees = [{
      "EmployeeID": "100A",
      "FirstName": "Downs",
      "aval": [
      {"start": "11-19", "end": "2", "ava": "30", "health": "4"},
      {"start": "11-20", "end": "2", "ava": "40", "health": "4"},
      {"start": "11-21", "end": "2", "ava": "50", "health": "4"},
      {"start": "11-22", "end": "2", "ava": "60", "health": "4"}
      ]
      },
      {
      "EmployeeID": "100B",
      "FirstName": "Mckenzie",
      "aval": [
      {"start": "11-19", "end": "2", "ava": "1", "health": "4"},
      {"start": "11-20", "end": "2", "ava": "2", "health": "4"},
      {"start": "11-21", "end": "2", "ava": "3", "health": "4"},
      {"start": "11-22", "end": "2", "ava": "4", "health": "4"}
      ]
      },

      ]


      and would like to get it like:



      const employees = [
      { "EmployeeID": "100A", "11-19": "30", "11-20": "40", "11-21": "50", "11-22": "60"},
      { "EmployeeID": "100B", "11-19": "1", "11-20": "2", "11-21": "3", "11-22": "4"}
      ]


      I've asked around and this gives me 10 objects instead of two, just need to put those pesky employeeID inside 1 object with all the start values altogether.



      const res = employees.reduce((acc, { EmployeeID, aval}) => [
      ...acc,
      ...aval.map( ({ start, ava}) => ({ EmployeeID, [start]: ava}) )
      ], );






      javascript arrays angular object reduce






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 22:50









      brohymnbrohymn

      157114




      157114
























          1 Answer
          1






          active

          oldest

          votes


















          3














          Because your input items and output items are one-to-one, you should use .map on employees instead of .reduce:






          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          Object.assign(
          { EmployeeID },
          ...aval.map(({ start, ava }) => ({ [start]: ava }))
          )
          ));
          console.log(res);





          Or, if you wanted to create fewer discarded intermediate objects, you could reduce the inner array aval instead of .map:






          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          aval.reduce((a, { start, ava }) => {
          a[start] = ava;
          return a;
          }, { EmployeeID })
          ));
          console.log(res);








          share|improve this answer


























          • Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?

            – brohymn
            Nov 24 '18 at 23:07













          • Learn reduce, as in, when to use it, or how to use it?

            – CertainPerformance
            Nov 24 '18 at 23:34











          • as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help

            – brohymn
            Nov 24 '18 at 23:36








          • 1





            When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only use reduce if neither .map nor .filter are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg (a, b) => a + b, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The pattern a[prop] = val; return a; is very common. You might search SO for answers by high-rep users containing reduce for examples

            – CertainPerformance
            Nov 24 '18 at 23:42











          • When you need to transform an array into a value (or multiple values), you can almost always use .reduce if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details

            – CertainPerformance
            Nov 24 '18 at 23:44











          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%2f53463041%2freduce-an-object-of-an-element-that-has-already-been-reduced%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Because your input items and output items are one-to-one, you should use .map on employees instead of .reduce:






          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          Object.assign(
          { EmployeeID },
          ...aval.map(({ start, ava }) => ({ [start]: ava }))
          )
          ));
          console.log(res);





          Or, if you wanted to create fewer discarded intermediate objects, you could reduce the inner array aval instead of .map:






          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          aval.reduce((a, { start, ava }) => {
          a[start] = ava;
          return a;
          }, { EmployeeID })
          ));
          console.log(res);








          share|improve this answer


























          • Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?

            – brohymn
            Nov 24 '18 at 23:07













          • Learn reduce, as in, when to use it, or how to use it?

            – CertainPerformance
            Nov 24 '18 at 23:34











          • as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help

            – brohymn
            Nov 24 '18 at 23:36








          • 1





            When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only use reduce if neither .map nor .filter are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg (a, b) => a + b, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The pattern a[prop] = val; return a; is very common. You might search SO for answers by high-rep users containing reduce for examples

            – CertainPerformance
            Nov 24 '18 at 23:42











          • When you need to transform an array into a value (or multiple values), you can almost always use .reduce if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details

            – CertainPerformance
            Nov 24 '18 at 23:44
















          3














          Because your input items and output items are one-to-one, you should use .map on employees instead of .reduce:






          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          Object.assign(
          { EmployeeID },
          ...aval.map(({ start, ava }) => ({ [start]: ava }))
          )
          ));
          console.log(res);





          Or, if you wanted to create fewer discarded intermediate objects, you could reduce the inner array aval instead of .map:






          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          aval.reduce((a, { start, ava }) => {
          a[start] = ava;
          return a;
          }, { EmployeeID })
          ));
          console.log(res);








          share|improve this answer


























          • Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?

            – brohymn
            Nov 24 '18 at 23:07













          • Learn reduce, as in, when to use it, or how to use it?

            – CertainPerformance
            Nov 24 '18 at 23:34











          • as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help

            – brohymn
            Nov 24 '18 at 23:36








          • 1





            When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only use reduce if neither .map nor .filter are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg (a, b) => a + b, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The pattern a[prop] = val; return a; is very common. You might search SO for answers by high-rep users containing reduce for examples

            – CertainPerformance
            Nov 24 '18 at 23:42











          • When you need to transform an array into a value (or multiple values), you can almost always use .reduce if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details

            – CertainPerformance
            Nov 24 '18 at 23:44














          3












          3








          3







          Because your input items and output items are one-to-one, you should use .map on employees instead of .reduce:






          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          Object.assign(
          { EmployeeID },
          ...aval.map(({ start, ava }) => ({ [start]: ava }))
          )
          ));
          console.log(res);





          Or, if you wanted to create fewer discarded intermediate objects, you could reduce the inner array aval instead of .map:






          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          aval.reduce((a, { start, ava }) => {
          a[start] = ava;
          return a;
          }, { EmployeeID })
          ));
          console.log(res);








          share|improve this answer















          Because your input items and output items are one-to-one, you should use .map on employees instead of .reduce:






          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          Object.assign(
          { EmployeeID },
          ...aval.map(({ start, ava }) => ({ [start]: ava }))
          )
          ));
          console.log(res);





          Or, if you wanted to create fewer discarded intermediate objects, you could reduce the inner array aval instead of .map:






          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          aval.reduce((a, { start, ava }) => {
          a[start] = ava;
          return a;
          }, { EmployeeID })
          ));
          console.log(res);








          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          Object.assign(
          { EmployeeID },
          ...aval.map(({ start, ava }) => ({ [start]: ava }))
          )
          ));
          console.log(res);





          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          Object.assign(
          { EmployeeID },
          ...aval.map(({ start, ava }) => ({ [start]: ava }))
          )
          ));
          console.log(res);





          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          aval.reduce((a, { start, ava }) => {
          a[start] = ava;
          return a;
          }, { EmployeeID })
          ));
          console.log(res);





          const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

          const res = employees.map(({ EmployeeID, aval }) => (
          aval.reduce((a, { start, ava }) => {
          a[start] = ava;
          return a;
          }, { EmployeeID })
          ));
          console.log(res);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 23:02

























          answered Nov 24 '18 at 22:56









          CertainPerformanceCertainPerformance

          81.5k143966




          81.5k143966













          • Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?

            – brohymn
            Nov 24 '18 at 23:07













          • Learn reduce, as in, when to use it, or how to use it?

            – CertainPerformance
            Nov 24 '18 at 23:34











          • as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help

            – brohymn
            Nov 24 '18 at 23:36








          • 1





            When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only use reduce if neither .map nor .filter are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg (a, b) => a + b, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The pattern a[prop] = val; return a; is very common. You might search SO for answers by high-rep users containing reduce for examples

            – CertainPerformance
            Nov 24 '18 at 23:42











          • When you need to transform an array into a value (or multiple values), you can almost always use .reduce if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details

            – CertainPerformance
            Nov 24 '18 at 23:44



















          • Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?

            – brohymn
            Nov 24 '18 at 23:07













          • Learn reduce, as in, when to use it, or how to use it?

            – CertainPerformance
            Nov 24 '18 at 23:34











          • as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help

            – brohymn
            Nov 24 '18 at 23:36








          • 1





            When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only use reduce if neither .map nor .filter are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg (a, b) => a + b, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The pattern a[prop] = val; return a; is very common. You might search SO for answers by high-rep users containing reduce for examples

            – CertainPerformance
            Nov 24 '18 at 23:42











          • When you need to transform an array into a value (or multiple values), you can almost always use .reduce if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details

            – CertainPerformance
            Nov 24 '18 at 23:44

















          Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?

          – brohymn
          Nov 24 '18 at 23:07







          Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?

          – brohymn
          Nov 24 '18 at 23:07















          Learn reduce, as in, when to use it, or how to use it?

          – CertainPerformance
          Nov 24 '18 at 23:34





          Learn reduce, as in, when to use it, or how to use it?

          – CertainPerformance
          Nov 24 '18 at 23:34













          as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help

          – brohymn
          Nov 24 '18 at 23:36







          as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help

          – brohymn
          Nov 24 '18 at 23:36






          1




          1





          When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only use reduce if neither .map nor .filter are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg (a, b) => a + b, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The pattern a[prop] = val; return a; is very common. You might search SO for answers by high-rep users containing reduce for examples

          – CertainPerformance
          Nov 24 '18 at 23:42





          When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only use reduce if neither .map nor .filter are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg (a, b) => a + b, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The pattern a[prop] = val; return a; is very common. You might search SO for answers by high-rep users containing reduce for examples

          – CertainPerformance
          Nov 24 '18 at 23:42













          When you need to transform an array into a value (or multiple values), you can almost always use .reduce if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details

          – CertainPerformance
          Nov 24 '18 at 23:44





          When you need to transform an array into a value (or multiple values), you can almost always use .reduce if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details

          – CertainPerformance
          Nov 24 '18 at 23:44


















          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%2f53463041%2freduce-an-object-of-an-element-that-has-already-been-reduced%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

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks