Slick way to create nested array types





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have something like this:



const rhs = ['Foo.Bar', 'Two', 'Three'];


and I want to get:



Array<Foo.Bar<Two<Three>>>


I have this which works:



       const literalType = rhs.reduce((a,b) => {
return [a,'<',b].join('');
});

const withBraces = literalType.concat(
new Array(rhs.length).fill(null).join('>')
);

const finalVal = `Array<${withBraces}>`,


But I am looking for something a little slicker than that. Does anyone have a good idea on how to make this simpler?










share|improve this question





























    1















    I have something like this:



    const rhs = ['Foo.Bar', 'Two', 'Three'];


    and I want to get:



    Array<Foo.Bar<Two<Three>>>


    I have this which works:



           const literalType = rhs.reduce((a,b) => {
    return [a,'<',b].join('');
    });

    const withBraces = literalType.concat(
    new Array(rhs.length).fill(null).join('>')
    );

    const finalVal = `Array<${withBraces}>`,


    But I am looking for something a little slicker than that. Does anyone have a good idea on how to make this simpler?










    share|improve this question

























      1












      1








      1








      I have something like this:



      const rhs = ['Foo.Bar', 'Two', 'Three'];


      and I want to get:



      Array<Foo.Bar<Two<Three>>>


      I have this which works:



             const literalType = rhs.reduce((a,b) => {
      return [a,'<',b].join('');
      });

      const withBraces = literalType.concat(
      new Array(rhs.length).fill(null).join('>')
      );

      const finalVal = `Array<${withBraces}>`,


      But I am looking for something a little slicker than that. Does anyone have a good idea on how to make this simpler?










      share|improve this question














      I have something like this:



      const rhs = ['Foo.Bar', 'Two', 'Three'];


      and I want to get:



      Array<Foo.Bar<Two<Three>>>


      I have this which works:



             const literalType = rhs.reduce((a,b) => {
      return [a,'<',b].join('');
      });

      const withBraces = literalType.concat(
      new Array(rhs.length).fill(null).join('>')
      );

      const finalVal = `Array<${withBraces}>`,


      But I am looking for something a little slicker than that. Does anyone have a good idea on how to make this simpler?







      javascript node.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '18 at 4:46









      MrCholoMrCholo

      1,6231036




      1,6231036
























          6 Answers
          6






          active

          oldest

          votes


















          1














          Combining two of the other answers, you can use reduceRight() and template literals to get something "slick":






          const rhs = ['Foo.Bar', 'Two', 'Three'];
          const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;

          console.log(finalVal);








          share|improve this answer































            2














            You can use "Array.reduce" like below to achieve this






            const rhs = ['Foo.Bar', 'Two', 'Three'];

            let res = rhs.reduce((s, d, i) =>
            (s += '<' + d
            , i == rhs.length -1 && (s += ('>'.repeat(i+1)))
            , s)
            , 'Array')

            console.log(res)








            share|improve this answer































              1














              You can use reduceRight which helps in going "outwards" in this case:






              const rhs = ['Foo.Bar', 'Two', 'Three'];

              const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
              console.log(res);








              share|improve this answer































                0














                I just reverse the array and build from the inside instead of the outside, then it's easier, see:



                const literalType = rhs.reverse().reduce((a,b) => {
                return [b,'<',a, '>'].join('');
                });

                const finalVal = `Array<${literalType}>`,





                share|improve this answer





















                • 1





                  reduceRight is also a function, and won't modify the original array.

                  – jhpratt
                  Nov 17 '18 at 5:39



















                0














                You can do something like this:






                const rhs = ['Foo.Bar', 'Two', 'Three'];

                const r = `Array<${rhs.reduce((r,c,i,a) =>
                `${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`

                console.log(r)





                The idea is to use the ES6 template literal hydration with Array.reduce since that method provides convenient access to the array index and the actual array internally.






                share|improve this answer

































                  0














                  We can also use simple constructs like,



                    str = "Array<" + rhs.join("<") + (">".repeat(rhs.length))
                  console.log(str)


                  Alternately, for empty array we can prepend first part "Array<" conditionally






                  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%2f53348307%2fslick-way-to-create-nested-array-types%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    6 Answers
                    6






                    active

                    oldest

                    votes








                    6 Answers
                    6






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    1














                    Combining two of the other answers, you can use reduceRight() and template literals to get something "slick":






                    const rhs = ['Foo.Bar', 'Two', 'Three'];
                    const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;

                    console.log(finalVal);








                    share|improve this answer




























                      1














                      Combining two of the other answers, you can use reduceRight() and template literals to get something "slick":






                      const rhs = ['Foo.Bar', 'Two', 'Three'];
                      const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;

                      console.log(finalVal);








                      share|improve this answer


























                        1












                        1








                        1







                        Combining two of the other answers, you can use reduceRight() and template literals to get something "slick":






                        const rhs = ['Foo.Bar', 'Two', 'Three'];
                        const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;

                        console.log(finalVal);








                        share|improve this answer













                        Combining two of the other answers, you can use reduceRight() and template literals to get something "slick":






                        const rhs = ['Foo.Bar', 'Two', 'Three'];
                        const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;

                        console.log(finalVal);








                        const rhs = ['Foo.Bar', 'Two', 'Three'];
                        const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;

                        console.log(finalVal);





                        const rhs = ['Foo.Bar', 'Two', 'Three'];
                        const finalVal = `Array${rhs.reduceRight((a, b) => `<${b}${a}>`, '')}`;

                        console.log(finalVal);






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 17 '18 at 7:41









                        Patrick RobertsPatrick Roberts

                        21.3k33878




                        21.3k33878

























                            2














                            You can use "Array.reduce" like below to achieve this






                            const rhs = ['Foo.Bar', 'Two', 'Three'];

                            let res = rhs.reduce((s, d, i) =>
                            (s += '<' + d
                            , i == rhs.length -1 && (s += ('>'.repeat(i+1)))
                            , s)
                            , 'Array')

                            console.log(res)








                            share|improve this answer




























                              2














                              You can use "Array.reduce" like below to achieve this






                              const rhs = ['Foo.Bar', 'Two', 'Three'];

                              let res = rhs.reduce((s, d, i) =>
                              (s += '<' + d
                              , i == rhs.length -1 && (s += ('>'.repeat(i+1)))
                              , s)
                              , 'Array')

                              console.log(res)








                              share|improve this answer


























                                2












                                2








                                2







                                You can use "Array.reduce" like below to achieve this






                                const rhs = ['Foo.Bar', 'Two', 'Three'];

                                let res = rhs.reduce((s, d, i) =>
                                (s += '<' + d
                                , i == rhs.length -1 && (s += ('>'.repeat(i+1)))
                                , s)
                                , 'Array')

                                console.log(res)








                                share|improve this answer













                                You can use "Array.reduce" like below to achieve this






                                const rhs = ['Foo.Bar', 'Two', 'Three'];

                                let res = rhs.reduce((s, d, i) =>
                                (s += '<' + d
                                , i == rhs.length -1 && (s += ('>'.repeat(i+1)))
                                , s)
                                , 'Array')

                                console.log(res)








                                const rhs = ['Foo.Bar', 'Two', 'Three'];

                                let res = rhs.reduce((s, d, i) =>
                                (s += '<' + d
                                , i == rhs.length -1 && (s += ('>'.repeat(i+1)))
                                , s)
                                , 'Array')

                                console.log(res)





                                const rhs = ['Foo.Bar', 'Two', 'Three'];

                                let res = rhs.reduce((s, d, i) =>
                                (s += '<' + d
                                , i == rhs.length -1 && (s += ('>'.repeat(i+1)))
                                , s)
                                , 'Array')

                                console.log(res)






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 17 '18 at 4:55









                                Nitish NarangNitish Narang

                                2,9801815




                                2,9801815























                                    1














                                    You can use reduceRight which helps in going "outwards" in this case:






                                    const rhs = ['Foo.Bar', 'Two', 'Three'];

                                    const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
                                    console.log(res);








                                    share|improve this answer




























                                      1














                                      You can use reduceRight which helps in going "outwards" in this case:






                                      const rhs = ['Foo.Bar', 'Two', 'Three'];

                                      const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
                                      console.log(res);








                                      share|improve this answer


























                                        1












                                        1








                                        1







                                        You can use reduceRight which helps in going "outwards" in this case:






                                        const rhs = ['Foo.Bar', 'Two', 'Three'];

                                        const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
                                        console.log(res);








                                        share|improve this answer













                                        You can use reduceRight which helps in going "outwards" in this case:






                                        const rhs = ['Foo.Bar', 'Two', 'Three'];

                                        const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
                                        console.log(res);








                                        const rhs = ['Foo.Bar', 'Two', 'Three'];

                                        const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
                                        console.log(res);





                                        const rhs = ['Foo.Bar', 'Two', 'Three'];

                                        const res = 'Array' + rhs.reduceRight((a, c) => `<${c + a}>`, '');
                                        console.log(res);






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 17 '18 at 5:18









                                        sliderslider

                                        8,56811331




                                        8,56811331























                                            0














                                            I just reverse the array and build from the inside instead of the outside, then it's easier, see:



                                            const literalType = rhs.reverse().reduce((a,b) => {
                                            return [b,'<',a, '>'].join('');
                                            });

                                            const finalVal = `Array<${literalType}>`,





                                            share|improve this answer





















                                            • 1





                                              reduceRight is also a function, and won't modify the original array.

                                              – jhpratt
                                              Nov 17 '18 at 5:39
















                                            0














                                            I just reverse the array and build from the inside instead of the outside, then it's easier, see:



                                            const literalType = rhs.reverse().reduce((a,b) => {
                                            return [b,'<',a, '>'].join('');
                                            });

                                            const finalVal = `Array<${literalType}>`,





                                            share|improve this answer





















                                            • 1





                                              reduceRight is also a function, and won't modify the original array.

                                              – jhpratt
                                              Nov 17 '18 at 5:39














                                            0












                                            0








                                            0







                                            I just reverse the array and build from the inside instead of the outside, then it's easier, see:



                                            const literalType = rhs.reverse().reduce((a,b) => {
                                            return [b,'<',a, '>'].join('');
                                            });

                                            const finalVal = `Array<${literalType}>`,





                                            share|improve this answer















                                            I just reverse the array and build from the inside instead of the outside, then it's easier, see:



                                            const literalType = rhs.reverse().reduce((a,b) => {
                                            return [b,'<',a, '>'].join('');
                                            });

                                            const finalVal = `Array<${literalType}>`,






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Nov 17 '18 at 4:50









                                            Nick Parsons

                                            10.5k21028




                                            10.5k21028










                                            answered Nov 17 '18 at 4:48









                                            MrCholoMrCholo

                                            1,6231036




                                            1,6231036








                                            • 1





                                              reduceRight is also a function, and won't modify the original array.

                                              – jhpratt
                                              Nov 17 '18 at 5:39














                                            • 1





                                              reduceRight is also a function, and won't modify the original array.

                                              – jhpratt
                                              Nov 17 '18 at 5:39








                                            1




                                            1





                                            reduceRight is also a function, and won't modify the original array.

                                            – jhpratt
                                            Nov 17 '18 at 5:39





                                            reduceRight is also a function, and won't modify the original array.

                                            – jhpratt
                                            Nov 17 '18 at 5:39











                                            0














                                            You can do something like this:






                                            const rhs = ['Foo.Bar', 'Two', 'Three'];

                                            const r = `Array<${rhs.reduce((r,c,i,a) =>
                                            `${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`

                                            console.log(r)





                                            The idea is to use the ES6 template literal hydration with Array.reduce since that method provides convenient access to the array index and the actual array internally.






                                            share|improve this answer






























                                              0














                                              You can do something like this:






                                              const rhs = ['Foo.Bar', 'Two', 'Three'];

                                              const r = `Array<${rhs.reduce((r,c,i,a) =>
                                              `${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`

                                              console.log(r)





                                              The idea is to use the ES6 template literal hydration with Array.reduce since that method provides convenient access to the array index and the actual array internally.






                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                You can do something like this:






                                                const rhs = ['Foo.Bar', 'Two', 'Three'];

                                                const r = `Array<${rhs.reduce((r,c,i,a) =>
                                                `${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`

                                                console.log(r)





                                                The idea is to use the ES6 template literal hydration with Array.reduce since that method provides convenient access to the array index and the actual array internally.






                                                share|improve this answer















                                                You can do something like this:






                                                const rhs = ['Foo.Bar', 'Two', 'Three'];

                                                const r = `Array<${rhs.reduce((r,c,i,a) =>
                                                `${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`

                                                console.log(r)





                                                The idea is to use the ES6 template literal hydration with Array.reduce since that method provides convenient access to the array index and the actual array internally.






                                                const rhs = ['Foo.Bar', 'Two', 'Three'];

                                                const r = `Array<${rhs.reduce((r,c,i,a) =>
                                                `${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`

                                                console.log(r)





                                                const rhs = ['Foo.Bar', 'Two', 'Three'];

                                                const r = `Array<${rhs.reduce((r,c,i,a) =>
                                                `${r}<${c}`.concat(i == a.length-1 ? '>'.repeat(i) : ''))}>`

                                                console.log(r)






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 17 '18 at 5:16

























                                                answered Nov 17 '18 at 5:11









                                                AkrionAkrion

                                                9,65011426




                                                9,65011426























                                                    0














                                                    We can also use simple constructs like,



                                                      str = "Array<" + rhs.join("<") + (">".repeat(rhs.length))
                                                    console.log(str)


                                                    Alternately, for empty array we can prepend first part "Array<" conditionally






                                                    share|improve this answer




























                                                      0














                                                      We can also use simple constructs like,



                                                        str = "Array<" + rhs.join("<") + (">".repeat(rhs.length))
                                                      console.log(str)


                                                      Alternately, for empty array we can prepend first part "Array<" conditionally






                                                      share|improve this answer


























                                                        0












                                                        0








                                                        0







                                                        We can also use simple constructs like,



                                                          str = "Array<" + rhs.join("<") + (">".repeat(rhs.length))
                                                        console.log(str)


                                                        Alternately, for empty array we can prepend first part "Array<" conditionally






                                                        share|improve this answer













                                                        We can also use simple constructs like,



                                                          str = "Array<" + rhs.join("<") + (">".repeat(rhs.length))
                                                        console.log(str)


                                                        Alternately, for empty array we can prepend first part "Array<" conditionally







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Nov 17 '18 at 7:14









                                                        MukundMukund

                                                        31117




                                                        31117






























                                                            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%2f53348307%2fslick-way-to-create-nested-array-types%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

                                                            Xamarin.iOS Cant Deploy on Iphone

                                                            Glorious Revolution

                                                            Dulmage-Mendelsohn matrix decomposition in Python