Assign array values to top level object javascript









up vote
0
down vote

favorite












At the bottom is a slimmed down version of a JSON file that I am trying to parse. I would like to create individual objects that have a key for the team name and the player name.



How would I go about using the team name and mapping to each individual player and receive something like this (using javascript):



[
name: 'Dallas Stars', playerName: 'Alexander Radulov',
name: 'Dallas Stars', playerName: 'Ben Bishop',
name: 'Dallas Stars', playerName: 'Jamie Benn'
...
name: 'Columbus Blue Jackets', playerName: 'Pierre-Luc Dubois'
]


From this JSON:



[ name: 'Dallas Stars',
roster:
[ 'Alexander Radulov',
'Ben Bishop',
'Jamie Benn',
'Tyler Pitlick',
'Miro Heiskanen' ] ,
name: 'Los Angeles Kings',
roster:
[ 'Jonathan Quick',
'Jonny Brodzinski',
'Oscar Fantenberg' ] ,
name: 'San Jose Sharks',
roster:
[ 'Joe Thornton',
'Brent Burns',
'Joe Pavelski',
'Antti Suomela' ] ,
name: 'Columbus Blue Jackets',
roster:
[ 'Sonny Milano',
'Brandon Dubinsky',
'Nick Foligno',
'Pierre-Luc Dubois' ] ]


Essentially I am trying to map a top level key pair to individual players. I have tried searching through all lodash functions as well and haven't stumbled upon the correct way to do this.



Is there a way to use a flat map and have the team name used multiple times?










share|improve this question

























    up vote
    0
    down vote

    favorite












    At the bottom is a slimmed down version of a JSON file that I am trying to parse. I would like to create individual objects that have a key for the team name and the player name.



    How would I go about using the team name and mapping to each individual player and receive something like this (using javascript):



    [
    name: 'Dallas Stars', playerName: 'Alexander Radulov',
    name: 'Dallas Stars', playerName: 'Ben Bishop',
    name: 'Dallas Stars', playerName: 'Jamie Benn'
    ...
    name: 'Columbus Blue Jackets', playerName: 'Pierre-Luc Dubois'
    ]


    From this JSON:



    [ name: 'Dallas Stars',
    roster:
    [ 'Alexander Radulov',
    'Ben Bishop',
    'Jamie Benn',
    'Tyler Pitlick',
    'Miro Heiskanen' ] ,
    name: 'Los Angeles Kings',
    roster:
    [ 'Jonathan Quick',
    'Jonny Brodzinski',
    'Oscar Fantenberg' ] ,
    name: 'San Jose Sharks',
    roster:
    [ 'Joe Thornton',
    'Brent Burns',
    'Joe Pavelski',
    'Antti Suomela' ] ,
    name: 'Columbus Blue Jackets',
    roster:
    [ 'Sonny Milano',
    'Brandon Dubinsky',
    'Nick Foligno',
    'Pierre-Luc Dubois' ] ]


    Essentially I am trying to map a top level key pair to individual players. I have tried searching through all lodash functions as well and haven't stumbled upon the correct way to do this.



    Is there a way to use a flat map and have the team name used multiple times?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      At the bottom is a slimmed down version of a JSON file that I am trying to parse. I would like to create individual objects that have a key for the team name and the player name.



      How would I go about using the team name and mapping to each individual player and receive something like this (using javascript):



      [
      name: 'Dallas Stars', playerName: 'Alexander Radulov',
      name: 'Dallas Stars', playerName: 'Ben Bishop',
      name: 'Dallas Stars', playerName: 'Jamie Benn'
      ...
      name: 'Columbus Blue Jackets', playerName: 'Pierre-Luc Dubois'
      ]


      From this JSON:



      [ name: 'Dallas Stars',
      roster:
      [ 'Alexander Radulov',
      'Ben Bishop',
      'Jamie Benn',
      'Tyler Pitlick',
      'Miro Heiskanen' ] ,
      name: 'Los Angeles Kings',
      roster:
      [ 'Jonathan Quick',
      'Jonny Brodzinski',
      'Oscar Fantenberg' ] ,
      name: 'San Jose Sharks',
      roster:
      [ 'Joe Thornton',
      'Brent Burns',
      'Joe Pavelski',
      'Antti Suomela' ] ,
      name: 'Columbus Blue Jackets',
      roster:
      [ 'Sonny Milano',
      'Brandon Dubinsky',
      'Nick Foligno',
      'Pierre-Luc Dubois' ] ]


      Essentially I am trying to map a top level key pair to individual players. I have tried searching through all lodash functions as well and haven't stumbled upon the correct way to do this.



      Is there a way to use a flat map and have the team name used multiple times?










      share|improve this question













      At the bottom is a slimmed down version of a JSON file that I am trying to parse. I would like to create individual objects that have a key for the team name and the player name.



      How would I go about using the team name and mapping to each individual player and receive something like this (using javascript):



      [
      name: 'Dallas Stars', playerName: 'Alexander Radulov',
      name: 'Dallas Stars', playerName: 'Ben Bishop',
      name: 'Dallas Stars', playerName: 'Jamie Benn'
      ...
      name: 'Columbus Blue Jackets', playerName: 'Pierre-Luc Dubois'
      ]


      From this JSON:



      [ name: 'Dallas Stars',
      roster:
      [ 'Alexander Radulov',
      'Ben Bishop',
      'Jamie Benn',
      'Tyler Pitlick',
      'Miro Heiskanen' ] ,
      name: 'Los Angeles Kings',
      roster:
      [ 'Jonathan Quick',
      'Jonny Brodzinski',
      'Oscar Fantenberg' ] ,
      name: 'San Jose Sharks',
      roster:
      [ 'Joe Thornton',
      'Brent Burns',
      'Joe Pavelski',
      'Antti Suomela' ] ,
      name: 'Columbus Blue Jackets',
      roster:
      [ 'Sonny Milano',
      'Brandon Dubinsky',
      'Nick Foligno',
      'Pierre-Luc Dubois' ] ]


      Essentially I am trying to map a top level key pair to individual players. I have tried searching through all lodash functions as well and haven't stumbled upon the correct way to do this.



      Is there a way to use a flat map and have the team name used multiple times?







      javascript json parsing dictionary key-value






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 2:04









      Stephen Phillips

      106




      106






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can also use map and flat.



          Map over the original array, then for each item being "mapped" map over its roster and create your desired object. Finally, since the resulting array will be 2d, flatten it:






          var data = [ name: 'Dallas Stars', roster: ['Alexander Radulov', 'Ben Bishop', 'Jamie Benn', 'Tyler Pitlick', 'Miro Heiskanen' ] , name: 'Los Angeles Kings', roster: ['Jonathan Quick', 'Jonny Brodzinski', 'Oscar Fantenberg' ] , name: 'San Jose Sharks', roster: ['Joe Thornton', 'Brent Burns', 'Joe Pavelski', 'Antti Suomela' ] , name: 'Columbus Blue Jackets', roster: ['Sonny Milano', 'Brandon Dubinsky', 'Nick Foligno', 'Pierre-Luc Dubois' ] ];

          var res = data
          .map((name, roster) =>
          roster.map(playerName => (name, playerName)))
          .flat();

          console.log(res);








          share|improve this answer


















          • 1




            Thanks so much! Wow this helped a lot. Didn't realize I could pass name into the second map again. How do I get around this "TypeError: data.map(...).flat is not a function". I read online some browsers don't support map, but for just running node app,js do I need to install a specific package?
            – Stephen Phillips
            Nov 12 at 15:41










          • Yes, it could be that flat isn't supported on the NodeJS version you're running. You can try using something like Babel (babeljs.io) to get around it.
            – slider
            Nov 12 at 16:49






          • 1




            I was running version 9.5. Switched to 11.1 and now it's working. Simple fix, great thanks!
            – Stephen Phillips
            Nov 13 at 2:32

















          up vote
          1
          down vote













          You need to iterate over the outer array items, and then inside of each of those, iterate over the roster too. reduce is usually the most appropriate method for transforming an array into another array on a non-one-to-one basis:






          const input=[name:'Dallas Stars',roster:['Alexander Radulov','Ben Bishop','Jamie Benn','Tyler Pitlick','Miro Heiskanen'],name:'Los Angeles Kings',roster:['Jonathan Quick','Jonny Brodzinski','Oscar Fantenberg'],name:'San Jose Sharks',roster:['Joe Thornton','Brent Burns','Joe Pavelski','Antti Suomela'],name:'Columbus Blue Jackets',roster:['Sonny Milano','Brandon Dubinsky','Nick Foligno','Pierre-Luc Dubois']];

          const output = input.reduce((a, name, roster ) =>
          roster.forEach((playerName) =>
          a.push( name, playerName );
          );
          return a;
          , );
          console.log(output);








          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%2f53255150%2fassign-array-values-to-top-level-object-javascript%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








            up vote
            0
            down vote



            accepted










            You can also use map and flat.



            Map over the original array, then for each item being "mapped" map over its roster and create your desired object. Finally, since the resulting array will be 2d, flatten it:






            var data = [ name: 'Dallas Stars', roster: ['Alexander Radulov', 'Ben Bishop', 'Jamie Benn', 'Tyler Pitlick', 'Miro Heiskanen' ] , name: 'Los Angeles Kings', roster: ['Jonathan Quick', 'Jonny Brodzinski', 'Oscar Fantenberg' ] , name: 'San Jose Sharks', roster: ['Joe Thornton', 'Brent Burns', 'Joe Pavelski', 'Antti Suomela' ] , name: 'Columbus Blue Jackets', roster: ['Sonny Milano', 'Brandon Dubinsky', 'Nick Foligno', 'Pierre-Luc Dubois' ] ];

            var res = data
            .map((name, roster) =>
            roster.map(playerName => (name, playerName)))
            .flat();

            console.log(res);








            share|improve this answer


















            • 1




              Thanks so much! Wow this helped a lot. Didn't realize I could pass name into the second map again. How do I get around this "TypeError: data.map(...).flat is not a function". I read online some browsers don't support map, but for just running node app,js do I need to install a specific package?
              – Stephen Phillips
              Nov 12 at 15:41










            • Yes, it could be that flat isn't supported on the NodeJS version you're running. You can try using something like Babel (babeljs.io) to get around it.
              – slider
              Nov 12 at 16:49






            • 1




              I was running version 9.5. Switched to 11.1 and now it's working. Simple fix, great thanks!
              – Stephen Phillips
              Nov 13 at 2:32














            up vote
            0
            down vote



            accepted










            You can also use map and flat.



            Map over the original array, then for each item being "mapped" map over its roster and create your desired object. Finally, since the resulting array will be 2d, flatten it:






            var data = [ name: 'Dallas Stars', roster: ['Alexander Radulov', 'Ben Bishop', 'Jamie Benn', 'Tyler Pitlick', 'Miro Heiskanen' ] , name: 'Los Angeles Kings', roster: ['Jonathan Quick', 'Jonny Brodzinski', 'Oscar Fantenberg' ] , name: 'San Jose Sharks', roster: ['Joe Thornton', 'Brent Burns', 'Joe Pavelski', 'Antti Suomela' ] , name: 'Columbus Blue Jackets', roster: ['Sonny Milano', 'Brandon Dubinsky', 'Nick Foligno', 'Pierre-Luc Dubois' ] ];

            var res = data
            .map((name, roster) =>
            roster.map(playerName => (name, playerName)))
            .flat();

            console.log(res);








            share|improve this answer


















            • 1




              Thanks so much! Wow this helped a lot. Didn't realize I could pass name into the second map again. How do I get around this "TypeError: data.map(...).flat is not a function". I read online some browsers don't support map, but for just running node app,js do I need to install a specific package?
              – Stephen Phillips
              Nov 12 at 15:41










            • Yes, it could be that flat isn't supported on the NodeJS version you're running. You can try using something like Babel (babeljs.io) to get around it.
              – slider
              Nov 12 at 16:49






            • 1




              I was running version 9.5. Switched to 11.1 and now it's working. Simple fix, great thanks!
              – Stephen Phillips
              Nov 13 at 2:32












            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            You can also use map and flat.



            Map over the original array, then for each item being "mapped" map over its roster and create your desired object. Finally, since the resulting array will be 2d, flatten it:






            var data = [ name: 'Dallas Stars', roster: ['Alexander Radulov', 'Ben Bishop', 'Jamie Benn', 'Tyler Pitlick', 'Miro Heiskanen' ] , name: 'Los Angeles Kings', roster: ['Jonathan Quick', 'Jonny Brodzinski', 'Oscar Fantenberg' ] , name: 'San Jose Sharks', roster: ['Joe Thornton', 'Brent Burns', 'Joe Pavelski', 'Antti Suomela' ] , name: 'Columbus Blue Jackets', roster: ['Sonny Milano', 'Brandon Dubinsky', 'Nick Foligno', 'Pierre-Luc Dubois' ] ];

            var res = data
            .map((name, roster) =>
            roster.map(playerName => (name, playerName)))
            .flat();

            console.log(res);








            share|improve this answer














            You can also use map and flat.



            Map over the original array, then for each item being "mapped" map over its roster and create your desired object. Finally, since the resulting array will be 2d, flatten it:






            var data = [ name: 'Dallas Stars', roster: ['Alexander Radulov', 'Ben Bishop', 'Jamie Benn', 'Tyler Pitlick', 'Miro Heiskanen' ] , name: 'Los Angeles Kings', roster: ['Jonathan Quick', 'Jonny Brodzinski', 'Oscar Fantenberg' ] , name: 'San Jose Sharks', roster: ['Joe Thornton', 'Brent Burns', 'Joe Pavelski', 'Antti Suomela' ] , name: 'Columbus Blue Jackets', roster: ['Sonny Milano', 'Brandon Dubinsky', 'Nick Foligno', 'Pierre-Luc Dubois' ] ];

            var res = data
            .map((name, roster) =>
            roster.map(playerName => (name, playerName)))
            .flat();

            console.log(res);








            var data = [ name: 'Dallas Stars', roster: ['Alexander Radulov', 'Ben Bishop', 'Jamie Benn', 'Tyler Pitlick', 'Miro Heiskanen' ] , name: 'Los Angeles Kings', roster: ['Jonathan Quick', 'Jonny Brodzinski', 'Oscar Fantenberg' ] , name: 'San Jose Sharks', roster: ['Joe Thornton', 'Brent Burns', 'Joe Pavelski', 'Antti Suomela' ] , name: 'Columbus Blue Jackets', roster: ['Sonny Milano', 'Brandon Dubinsky', 'Nick Foligno', 'Pierre-Luc Dubois' ] ];

            var res = data
            .map((name, roster) =>
            roster.map(playerName => (name, playerName)))
            .flat();

            console.log(res);





            var data = [ name: 'Dallas Stars', roster: ['Alexander Radulov', 'Ben Bishop', 'Jamie Benn', 'Tyler Pitlick', 'Miro Heiskanen' ] , name: 'Los Angeles Kings', roster: ['Jonathan Quick', 'Jonny Brodzinski', 'Oscar Fantenberg' ] , name: 'San Jose Sharks', roster: ['Joe Thornton', 'Brent Burns', 'Joe Pavelski', 'Antti Suomela' ] , name: 'Columbus Blue Jackets', roster: ['Sonny Milano', 'Brandon Dubinsky', 'Nick Foligno', 'Pierre-Luc Dubois' ] ];

            var res = data
            .map((name, roster) =>
            roster.map(playerName => (name, playerName)))
            .flat();

            console.log(res);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 12 at 2:20

























            answered Nov 12 at 2:14









            slider

            8,0051129




            8,0051129







            • 1




              Thanks so much! Wow this helped a lot. Didn't realize I could pass name into the second map again. How do I get around this "TypeError: data.map(...).flat is not a function". I read online some browsers don't support map, but for just running node app,js do I need to install a specific package?
              – Stephen Phillips
              Nov 12 at 15:41










            • Yes, it could be that flat isn't supported on the NodeJS version you're running. You can try using something like Babel (babeljs.io) to get around it.
              – slider
              Nov 12 at 16:49






            • 1




              I was running version 9.5. Switched to 11.1 and now it's working. Simple fix, great thanks!
              – Stephen Phillips
              Nov 13 at 2:32












            • 1




              Thanks so much! Wow this helped a lot. Didn't realize I could pass name into the second map again. How do I get around this "TypeError: data.map(...).flat is not a function". I read online some browsers don't support map, but for just running node app,js do I need to install a specific package?
              – Stephen Phillips
              Nov 12 at 15:41










            • Yes, it could be that flat isn't supported on the NodeJS version you're running. You can try using something like Babel (babeljs.io) to get around it.
              – slider
              Nov 12 at 16:49






            • 1




              I was running version 9.5. Switched to 11.1 and now it's working. Simple fix, great thanks!
              – Stephen Phillips
              Nov 13 at 2:32







            1




            1




            Thanks so much! Wow this helped a lot. Didn't realize I could pass name into the second map again. How do I get around this "TypeError: data.map(...).flat is not a function". I read online some browsers don't support map, but for just running node app,js do I need to install a specific package?
            – Stephen Phillips
            Nov 12 at 15:41




            Thanks so much! Wow this helped a lot. Didn't realize I could pass name into the second map again. How do I get around this "TypeError: data.map(...).flat is not a function". I read online some browsers don't support map, but for just running node app,js do I need to install a specific package?
            – Stephen Phillips
            Nov 12 at 15:41












            Yes, it could be that flat isn't supported on the NodeJS version you're running. You can try using something like Babel (babeljs.io) to get around it.
            – slider
            Nov 12 at 16:49




            Yes, it could be that flat isn't supported on the NodeJS version you're running. You can try using something like Babel (babeljs.io) to get around it.
            – slider
            Nov 12 at 16:49




            1




            1




            I was running version 9.5. Switched to 11.1 and now it's working. Simple fix, great thanks!
            – Stephen Phillips
            Nov 13 at 2:32




            I was running version 9.5. Switched to 11.1 and now it's working. Simple fix, great thanks!
            – Stephen Phillips
            Nov 13 at 2:32












            up vote
            1
            down vote













            You need to iterate over the outer array items, and then inside of each of those, iterate over the roster too. reduce is usually the most appropriate method for transforming an array into another array on a non-one-to-one basis:






            const input=[name:'Dallas Stars',roster:['Alexander Radulov','Ben Bishop','Jamie Benn','Tyler Pitlick','Miro Heiskanen'],name:'Los Angeles Kings',roster:['Jonathan Quick','Jonny Brodzinski','Oscar Fantenberg'],name:'San Jose Sharks',roster:['Joe Thornton','Brent Burns','Joe Pavelski','Antti Suomela'],name:'Columbus Blue Jackets',roster:['Sonny Milano','Brandon Dubinsky','Nick Foligno','Pierre-Luc Dubois']];

            const output = input.reduce((a, name, roster ) =>
            roster.forEach((playerName) =>
            a.push( name, playerName );
            );
            return a;
            , );
            console.log(output);








            share|improve this answer
























              up vote
              1
              down vote













              You need to iterate over the outer array items, and then inside of each of those, iterate over the roster too. reduce is usually the most appropriate method for transforming an array into another array on a non-one-to-one basis:






              const input=[name:'Dallas Stars',roster:['Alexander Radulov','Ben Bishop','Jamie Benn','Tyler Pitlick','Miro Heiskanen'],name:'Los Angeles Kings',roster:['Jonathan Quick','Jonny Brodzinski','Oscar Fantenberg'],name:'San Jose Sharks',roster:['Joe Thornton','Brent Burns','Joe Pavelski','Antti Suomela'],name:'Columbus Blue Jackets',roster:['Sonny Milano','Brandon Dubinsky','Nick Foligno','Pierre-Luc Dubois']];

              const output = input.reduce((a, name, roster ) =>
              roster.forEach((playerName) =>
              a.push( name, playerName );
              );
              return a;
              , );
              console.log(output);








              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                You need to iterate over the outer array items, and then inside of each of those, iterate over the roster too. reduce is usually the most appropriate method for transforming an array into another array on a non-one-to-one basis:






                const input=[name:'Dallas Stars',roster:['Alexander Radulov','Ben Bishop','Jamie Benn','Tyler Pitlick','Miro Heiskanen'],name:'Los Angeles Kings',roster:['Jonathan Quick','Jonny Brodzinski','Oscar Fantenberg'],name:'San Jose Sharks',roster:['Joe Thornton','Brent Burns','Joe Pavelski','Antti Suomela'],name:'Columbus Blue Jackets',roster:['Sonny Milano','Brandon Dubinsky','Nick Foligno','Pierre-Luc Dubois']];

                const output = input.reduce((a, name, roster ) =>
                roster.forEach((playerName) =>
                a.push( name, playerName );
                );
                return a;
                , );
                console.log(output);








                share|improve this answer












                You need to iterate over the outer array items, and then inside of each of those, iterate over the roster too. reduce is usually the most appropriate method for transforming an array into another array on a non-one-to-one basis:






                const input=[name:'Dallas Stars',roster:['Alexander Radulov','Ben Bishop','Jamie Benn','Tyler Pitlick','Miro Heiskanen'],name:'Los Angeles Kings',roster:['Jonathan Quick','Jonny Brodzinski','Oscar Fantenberg'],name:'San Jose Sharks',roster:['Joe Thornton','Brent Burns','Joe Pavelski','Antti Suomela'],name:'Columbus Blue Jackets',roster:['Sonny Milano','Brandon Dubinsky','Nick Foligno','Pierre-Luc Dubois']];

                const output = input.reduce((a, name, roster ) =>
                roster.forEach((playerName) =>
                a.push( name, playerName );
                );
                return a;
                , );
                console.log(output);








                const input=[name:'Dallas Stars',roster:['Alexander Radulov','Ben Bishop','Jamie Benn','Tyler Pitlick','Miro Heiskanen'],name:'Los Angeles Kings',roster:['Jonathan Quick','Jonny Brodzinski','Oscar Fantenberg'],name:'San Jose Sharks',roster:['Joe Thornton','Brent Burns','Joe Pavelski','Antti Suomela'],name:'Columbus Blue Jackets',roster:['Sonny Milano','Brandon Dubinsky','Nick Foligno','Pierre-Luc Dubois']];

                const output = input.reduce((a, name, roster ) =>
                roster.forEach((playerName) =>
                a.push( name, playerName );
                );
                return a;
                , );
                console.log(output);





                const input=[name:'Dallas Stars',roster:['Alexander Radulov','Ben Bishop','Jamie Benn','Tyler Pitlick','Miro Heiskanen'],name:'Los Angeles Kings',roster:['Jonathan Quick','Jonny Brodzinski','Oscar Fantenberg'],name:'San Jose Sharks',roster:['Joe Thornton','Brent Burns','Joe Pavelski','Antti Suomela'],name:'Columbus Blue Jackets',roster:['Sonny Milano','Brandon Dubinsky','Nick Foligno','Pierre-Luc Dubois']];

                const output = input.reduce((a, name, roster ) =>
                roster.forEach((playerName) =>
                a.push( name, playerName );
                );
                return a;
                , );
                console.log(output);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 2:06









                CertainPerformance

                72.9k143454




                72.9k143454



























                    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%2f53255150%2fassign-array-values-to-top-level-object-javascript%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

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

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto