Convert arrays of arrays to object with key

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











up vote
6
down vote

favorite












I have arrays of arrays which contains something like this:



var values = [[1, 2, 3], [3, 2, 1]]


I've tried .map() and Object.assign but I dont know how to implement it. I want this as an output:



values = [
{'up': 1, 'middle': 2, 'down': 3],
...
]


What should I use? This is what Im came up so far:



const object1 = [[1,2,3],[3,2,1]],
object =

object1.forEach(function(array)
object.map(value => ('up': array[0], 'mid': array[1], 'down': array[2]))
);

console.log(object)









share|improve this question























  • what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
    – Farooq Khan
    2 hours ago















up vote
6
down vote

favorite












I have arrays of arrays which contains something like this:



var values = [[1, 2, 3], [3, 2, 1]]


I've tried .map() and Object.assign but I dont know how to implement it. I want this as an output:



values = [
{'up': 1, 'middle': 2, 'down': 3],
...
]


What should I use? This is what Im came up so far:



const object1 = [[1,2,3],[3,2,1]],
object =

object1.forEach(function(array)
object.map(value => ('up': array[0], 'mid': array[1], 'down': array[2]))
);

console.log(object)









share|improve this question























  • what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
    – Farooq Khan
    2 hours ago













up vote
6
down vote

favorite









up vote
6
down vote

favorite











I have arrays of arrays which contains something like this:



var values = [[1, 2, 3], [3, 2, 1]]


I've tried .map() and Object.assign but I dont know how to implement it. I want this as an output:



values = [
{'up': 1, 'middle': 2, 'down': 3],
...
]


What should I use? This is what Im came up so far:



const object1 = [[1,2,3],[3,2,1]],
object =

object1.forEach(function(array)
object.map(value => ('up': array[0], 'mid': array[1], 'down': array[2]))
);

console.log(object)









share|improve this question















I have arrays of arrays which contains something like this:



var values = [[1, 2, 3], [3, 2, 1]]


I've tried .map() and Object.assign but I dont know how to implement it. I want this as an output:



values = [
{'up': 1, 'middle': 2, 'down': 3],
...
]


What should I use? This is what Im came up so far:



const object1 = [[1,2,3],[3,2,1]],
object =

object1.forEach(function(array)
object.map(value => ('up': array[0], 'mid': array[1], 'down': array[2]))
);

console.log(object)






javascript arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago









Farooq Khan

1,16411029




1,16411029










asked 3 hours ago









Cheryl Blossom

487




487











  • what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
    – Farooq Khan
    2 hours ago

















  • what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
    – Farooq Khan
    2 hours ago
















what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
– Farooq Khan
2 hours ago





what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
– Farooq Khan
2 hours ago













3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










Not very different from what others already did, but a little more elegant:






let arr = [[1, 2, 3], [3, 2, 1]];

let result = arr.map(([up, middle, down]) => (up, middle, down));
console.log(result);








share|improve this answer



























    up vote
    3
    down vote













    you can simply use Array.map(), there is no need of forEach()






    let arr =[[1, 2, 3], [3, 2, 1]];

    let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
    console.log(result);








    share|improve this answer



























      up vote
      0
      down vote













      You can try this simple:



      var values = [[1, 2, 3], [3, 2, 1]];

      if(values.length)
      values.forEach(function(valueArray, index)
      var temp = ;
      valueArray.forEach(function(value)
      if(value == 1)temp.up = value;
      if(value == 2)temp.middle = value;
      if(value == 3)temp.down = value;
      );
      values[index] = temp;
      );


      console.log(values);





      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',
        convertImagesToLinks: true,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        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%2f53002477%2fconvert-arrays-of-arrays-to-object-with-key%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote



        accepted










        Not very different from what others already did, but a little more elegant:






        let arr = [[1, 2, 3], [3, 2, 1]];

        let result = arr.map(([up, middle, down]) => (up, middle, down));
        console.log(result);








        share|improve this answer
























          up vote
          4
          down vote



          accepted










          Not very different from what others already did, but a little more elegant:






          let arr = [[1, 2, 3], [3, 2, 1]];

          let result = arr.map(([up, middle, down]) => (up, middle, down));
          console.log(result);








          share|improve this answer






















            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            Not very different from what others already did, but a little more elegant:






            let arr = [[1, 2, 3], [3, 2, 1]];

            let result = arr.map(([up, middle, down]) => (up, middle, down));
            console.log(result);








            share|improve this answer












            Not very different from what others already did, but a little more elegant:






            let arr = [[1, 2, 3], [3, 2, 1]];

            let result = arr.map(([up, middle, down]) => (up, middle, down));
            console.log(result);








            let arr = [[1, 2, 3], [3, 2, 1]];

            let result = arr.map(([up, middle, down]) => (up, middle, down));
            console.log(result);





            let arr = [[1, 2, 3], [3, 2, 1]];

            let result = arr.map(([up, middle, down]) => (up, middle, down));
            console.log(result);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 hours ago









            Amit

            34.9k74478




            34.9k74478






















                up vote
                3
                down vote













                you can simply use Array.map(), there is no need of forEach()






                let arr =[[1, 2, 3], [3, 2, 1]];

                let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
                console.log(result);








                share|improve this answer
























                  up vote
                  3
                  down vote













                  you can simply use Array.map(), there is no need of forEach()






                  let arr =[[1, 2, 3], [3, 2, 1]];

                  let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
                  console.log(result);








                  share|improve this answer






















                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    you can simply use Array.map(), there is no need of forEach()






                    let arr =[[1, 2, 3], [3, 2, 1]];

                    let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
                    console.log(result);








                    share|improve this answer












                    you can simply use Array.map(), there is no need of forEach()






                    let arr =[[1, 2, 3], [3, 2, 1]];

                    let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
                    console.log(result);








                    let arr =[[1, 2, 3], [3, 2, 1]];

                    let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
                    console.log(result);





                    let arr =[[1, 2, 3], [3, 2, 1]];

                    let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
                    console.log(result);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 2 hours ago









                    amrender singh

                    4,7361518




                    4,7361518




















                        up vote
                        0
                        down vote













                        You can try this simple:



                        var values = [[1, 2, 3], [3, 2, 1]];

                        if(values.length)
                        values.forEach(function(valueArray, index)
                        var temp = ;
                        valueArray.forEach(function(value)
                        if(value == 1)temp.up = value;
                        if(value == 2)temp.middle = value;
                        if(value == 3)temp.down = value;
                        );
                        values[index] = temp;
                        );


                        console.log(values);





                        share|improve this answer
























                          up vote
                          0
                          down vote













                          You can try this simple:



                          var values = [[1, 2, 3], [3, 2, 1]];

                          if(values.length)
                          values.forEach(function(valueArray, index)
                          var temp = ;
                          valueArray.forEach(function(value)
                          if(value == 1)temp.up = value;
                          if(value == 2)temp.middle = value;
                          if(value == 3)temp.down = value;
                          );
                          values[index] = temp;
                          );


                          console.log(values);





                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You can try this simple:



                            var values = [[1, 2, 3], [3, 2, 1]];

                            if(values.length)
                            values.forEach(function(valueArray, index)
                            var temp = ;
                            valueArray.forEach(function(value)
                            if(value == 1)temp.up = value;
                            if(value == 2)temp.middle = value;
                            if(value == 3)temp.down = value;
                            );
                            values[index] = temp;
                            );


                            console.log(values);





                            share|improve this answer












                            You can try this simple:



                            var values = [[1, 2, 3], [3, 2, 1]];

                            if(values.length)
                            values.forEach(function(valueArray, index)
                            var temp = ;
                            valueArray.forEach(function(value)
                            if(value == 1)temp.up = value;
                            if(value == 2)temp.middle = value;
                            if(value == 3)temp.down = value;
                            );
                            values[index] = temp;
                            );


                            console.log(values);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 hours ago









                            Nitin Kumar

                            405




                            405



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53002477%2fconvert-arrays-of-arrays-to-object-with-key%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                Confectionery