JS: writing a function that has an input of a multidimensional array











up vote
-3
down vote

favorite












I want to input an array of arrays, but then pick them apart. so for example I have an array of locations so like multiple coordinates of latitude and longitude.
but I want to write a loop that will then take that array of arrays and make and array for all of the latitude coordinates and all of the longitude coordinates.



so for example if I had



input = [[45,45],[35,75][85,90]]
it would make 2 arrays as my output
[45,35,85]
and
[45,75,90]










share|improve this question


















  • 4




    What is your your question? Where are you having problems?
    – slider
    Nov 8 at 18:23










  • What you are looking for is a thing called zip, or zipWith
    – bugs
    Nov 8 at 18:23






  • 1




    Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
    – Andy
    Nov 8 at 18:27















up vote
-3
down vote

favorite












I want to input an array of arrays, but then pick them apart. so for example I have an array of locations so like multiple coordinates of latitude and longitude.
but I want to write a loop that will then take that array of arrays and make and array for all of the latitude coordinates and all of the longitude coordinates.



so for example if I had



input = [[45,45],[35,75][85,90]]
it would make 2 arrays as my output
[45,35,85]
and
[45,75,90]










share|improve this question


















  • 4




    What is your your question? Where are you having problems?
    – slider
    Nov 8 at 18:23










  • What you are looking for is a thing called zip, or zipWith
    – bugs
    Nov 8 at 18:23






  • 1




    Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
    – Andy
    Nov 8 at 18:27













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I want to input an array of arrays, but then pick them apart. so for example I have an array of locations so like multiple coordinates of latitude and longitude.
but I want to write a loop that will then take that array of arrays and make and array for all of the latitude coordinates and all of the longitude coordinates.



so for example if I had



input = [[45,45],[35,75][85,90]]
it would make 2 arrays as my output
[45,35,85]
and
[45,75,90]










share|improve this question













I want to input an array of arrays, but then pick them apart. so for example I have an array of locations so like multiple coordinates of latitude and longitude.
but I want to write a loop that will then take that array of arrays and make and array for all of the latitude coordinates and all of the longitude coordinates.



so for example if I had



input = [[45,45],[35,75][85,90]]
it would make 2 arrays as my output
[45,35,85]
and
[45,75,90]







javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 18:21









Zach Lyness SonicEX

11




11








  • 4




    What is your your question? Where are you having problems?
    – slider
    Nov 8 at 18:23










  • What you are looking for is a thing called zip, or zipWith
    – bugs
    Nov 8 at 18:23






  • 1




    Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
    – Andy
    Nov 8 at 18:27














  • 4




    What is your your question? Where are you having problems?
    – slider
    Nov 8 at 18:23










  • What you are looking for is a thing called zip, or zipWith
    – bugs
    Nov 8 at 18:23






  • 1




    Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
    – Andy
    Nov 8 at 18:27








4




4




What is your your question? Where are you having problems?
– slider
Nov 8 at 18:23




What is your your question? Where are you having problems?
– slider
Nov 8 at 18:23












What you are looking for is a thing called zip, or zipWith
– bugs
Nov 8 at 18:23




What you are looking for is a thing called zip, or zipWith
– bugs
Nov 8 at 18:23




1




1




Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
– Andy
Nov 8 at 18:27




Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
– Andy
Nov 8 at 18:27












3 Answers
3






active

oldest

votes

















up vote
0
down vote













You could transpose the array and take lat and long as single arrays.






var input = [[45, 45], [35, 75], [85, 90]],
[lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );

console.log(lat);
console.log(long);

.as-console-wrapper { max-height: 100% !important; top: 0; }








share|improve this answer




























    up vote
    -1
    down vote













    Try this, loops over every coordinate and puts the first value from each coordinate into the array called first and the second value from each coordinate into an array called second ..



    var input = [[45,45],[35,75],[85,90]];

    function splitValues(coordinates) {
    var first = ;
    var second = ;
    for (var i = 0; i < coordinates.length; i++) {
    first.push(coordinates[i][0]);
    second.push(coordinates[i][1]);
    }
    }

    splitValues(input);





    share|improve this answer




























      up vote
      -1
      down vote













      This will help assuming you will always have a 2 values array in input and you want only 2 results



      const array = [[45,45],[35,75],[85,90]]
      let first =
      let second =
      array.forEach((item)=>{
      first.push(item[0])
      second.push(item[1])
      })
      console.log(first)
      console.log(second)





      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: 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%2f53213912%2fjs-writing-a-function-that-has-an-input-of-a-multidimensional-array%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote













        You could transpose the array and take lat and long as single arrays.






        var input = [[45, 45], [35, 75], [85, 90]],
        [lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );

        console.log(lat);
        console.log(long);

        .as-console-wrapper { max-height: 100% !important; top: 0; }








        share|improve this answer

























          up vote
          0
          down vote













          You could transpose the array and take lat and long as single arrays.






          var input = [[45, 45], [35, 75], [85, 90]],
          [lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );

          console.log(lat);
          console.log(long);

          .as-console-wrapper { max-height: 100% !important; top: 0; }








          share|improve this answer























            up vote
            0
            down vote










            up vote
            0
            down vote









            You could transpose the array and take lat and long as single arrays.






            var input = [[45, 45], [35, 75], [85, 90]],
            [lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );

            console.log(lat);
            console.log(long);

            .as-console-wrapper { max-height: 100% !important; top: 0; }








            share|improve this answer












            You could transpose the array and take lat and long as single arrays.






            var input = [[45, 45], [35, 75], [85, 90]],
            [lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );

            console.log(lat);
            console.log(long);

            .as-console-wrapper { max-height: 100% !important; top: 0; }








            var input = [[45, 45], [35, 75], [85, 90]],
            [lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );

            console.log(lat);
            console.log(long);

            .as-console-wrapper { max-height: 100% !important; top: 0; }





            var input = [[45, 45], [35, 75], [85, 90]],
            [lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );

            console.log(lat);
            console.log(long);

            .as-console-wrapper { max-height: 100% !important; top: 0; }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 at 18:44









            Nina Scholz

            172k1384147




            172k1384147
























                up vote
                -1
                down vote













                Try this, loops over every coordinate and puts the first value from each coordinate into the array called first and the second value from each coordinate into an array called second ..



                var input = [[45,45],[35,75],[85,90]];

                function splitValues(coordinates) {
                var first = ;
                var second = ;
                for (var i = 0; i < coordinates.length; i++) {
                first.push(coordinates[i][0]);
                second.push(coordinates[i][1]);
                }
                }

                splitValues(input);





                share|improve this answer

























                  up vote
                  -1
                  down vote













                  Try this, loops over every coordinate and puts the first value from each coordinate into the array called first and the second value from each coordinate into an array called second ..



                  var input = [[45,45],[35,75],[85,90]];

                  function splitValues(coordinates) {
                  var first = ;
                  var second = ;
                  for (var i = 0; i < coordinates.length; i++) {
                  first.push(coordinates[i][0]);
                  second.push(coordinates[i][1]);
                  }
                  }

                  splitValues(input);





                  share|improve this answer























                    up vote
                    -1
                    down vote










                    up vote
                    -1
                    down vote









                    Try this, loops over every coordinate and puts the first value from each coordinate into the array called first and the second value from each coordinate into an array called second ..



                    var input = [[45,45],[35,75],[85,90]];

                    function splitValues(coordinates) {
                    var first = ;
                    var second = ;
                    for (var i = 0; i < coordinates.length; i++) {
                    first.push(coordinates[i][0]);
                    second.push(coordinates[i][1]);
                    }
                    }

                    splitValues(input);





                    share|improve this answer












                    Try this, loops over every coordinate and puts the first value from each coordinate into the array called first and the second value from each coordinate into an array called second ..



                    var input = [[45,45],[35,75],[85,90]];

                    function splitValues(coordinates) {
                    var first = ;
                    var second = ;
                    for (var i = 0; i < coordinates.length; i++) {
                    first.push(coordinates[i][0]);
                    second.push(coordinates[i][1]);
                    }
                    }

                    splitValues(input);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 8 at 18:26









                    bobbyrne01

                    2,09343891




                    2,09343891






















                        up vote
                        -1
                        down vote













                        This will help assuming you will always have a 2 values array in input and you want only 2 results



                        const array = [[45,45],[35,75],[85,90]]
                        let first =
                        let second =
                        array.forEach((item)=>{
                        first.push(item[0])
                        second.push(item[1])
                        })
                        console.log(first)
                        console.log(second)





                        share|improve this answer

























                          up vote
                          -1
                          down vote













                          This will help assuming you will always have a 2 values array in input and you want only 2 results



                          const array = [[45,45],[35,75],[85,90]]
                          let first =
                          let second =
                          array.forEach((item)=>{
                          first.push(item[0])
                          second.push(item[1])
                          })
                          console.log(first)
                          console.log(second)





                          share|improve this answer























                            up vote
                            -1
                            down vote










                            up vote
                            -1
                            down vote









                            This will help assuming you will always have a 2 values array in input and you want only 2 results



                            const array = [[45,45],[35,75],[85,90]]
                            let first =
                            let second =
                            array.forEach((item)=>{
                            first.push(item[0])
                            second.push(item[1])
                            })
                            console.log(first)
                            console.log(second)





                            share|improve this answer












                            This will help assuming you will always have a 2 values array in input and you want only 2 results



                            const array = [[45,45],[35,75],[85,90]]
                            let first =
                            let second =
                            array.forEach((item)=>{
                            first.push(item[0])
                            second.push(item[1])
                            })
                            console.log(first)
                            console.log(second)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 8 at 18:27









                            Exequiel Aguirre

                            56527




                            56527






























                                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%2f53213912%2fjs-writing-a-function-that-has-an-input-of-a-multidimensional-array%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







                                這個網誌中的熱門文章

                                Tangent Lines Diagram Along Smooth Curve

                                Yusuf al-Mu'taman ibn Hud

                                Zucchini