Angularjs split array











up vote
0
down vote

favorite












I have an array that looks like this



    $scope.A = [

[ "1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526" ],
[ "1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632" ]

];


I want to make it into something like this



        $scope.B = [
[
[1.31069258855609,103.848649478524], [1.31138534529796,103.848923050526]
],
[
[1.31213221536436,103.848328363879], [1.31288473199114,103.849575392632]
]
];


PS: Sorry if this isnt splitting, I am not sure how to address this problem as



Code:



    var latlngs = $scope.polyLineCord.map(subarr => subarr.map(str => str.split(',').map(Number)))
console.log(latlngs)


enter image description here










share|improve this question









New contributor




ITnyp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Fetched from an api
    – ITnyp
    Nov 8 at 9:16










  • I am trying to plot a polyline in my map
    – ITnyp
    Nov 8 at 9:17










  • Ohh, sorry it was a typo edited it
    – ITnyp
    Nov 8 at 9:18















up vote
0
down vote

favorite












I have an array that looks like this



    $scope.A = [

[ "1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526" ],
[ "1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632" ]

];


I want to make it into something like this



        $scope.B = [
[
[1.31069258855609,103.848649478524], [1.31138534529796,103.848923050526]
],
[
[1.31213221536436,103.848328363879], [1.31288473199114,103.849575392632]
]
];


PS: Sorry if this isnt splitting, I am not sure how to address this problem as



Code:



    var latlngs = $scope.polyLineCord.map(subarr => subarr.map(str => str.split(',').map(Number)))
console.log(latlngs)


enter image description here










share|improve this question









New contributor




ITnyp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Fetched from an api
    – ITnyp
    Nov 8 at 9:16










  • I am trying to plot a polyline in my map
    – ITnyp
    Nov 8 at 9:17










  • Ohh, sorry it was a typo edited it
    – ITnyp
    Nov 8 at 9:18













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an array that looks like this



    $scope.A = [

[ "1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526" ],
[ "1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632" ]

];


I want to make it into something like this



        $scope.B = [
[
[1.31069258855609,103.848649478524], [1.31138534529796,103.848923050526]
],
[
[1.31213221536436,103.848328363879], [1.31288473199114,103.849575392632]
]
];


PS: Sorry if this isnt splitting, I am not sure how to address this problem as



Code:



    var latlngs = $scope.polyLineCord.map(subarr => subarr.map(str => str.split(',').map(Number)))
console.log(latlngs)


enter image description here










share|improve this question









New contributor




ITnyp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have an array that looks like this



    $scope.A = [

[ "1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526" ],
[ "1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632" ]

];


I want to make it into something like this



        $scope.B = [
[
[1.31069258855609,103.848649478524], [1.31138534529796,103.848923050526]
],
[
[1.31213221536436,103.848328363879], [1.31288473199114,103.849575392632]
]
];


PS: Sorry if this isnt splitting, I am not sure how to address this problem as



Code:



    var latlngs = $scope.polyLineCord.map(subarr => subarr.map(str => str.split(',').map(Number)))
console.log(latlngs)


enter image description here







javascript angularjs






share|improve this question









New contributor




ITnyp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




ITnyp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 8 at 9:36





















New contributor




ITnyp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 8 at 9:15









ITnyp

205




205




New contributor




ITnyp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





ITnyp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






ITnyp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Fetched from an api
    – ITnyp
    Nov 8 at 9:16










  • I am trying to plot a polyline in my map
    – ITnyp
    Nov 8 at 9:17










  • Ohh, sorry it was a typo edited it
    – ITnyp
    Nov 8 at 9:18


















  • Fetched from an api
    – ITnyp
    Nov 8 at 9:16










  • I am trying to plot a polyline in my map
    – ITnyp
    Nov 8 at 9:17










  • Ohh, sorry it was a typo edited it
    – ITnyp
    Nov 8 at 9:18
















Fetched from an api
– ITnyp
Nov 8 at 9:16




Fetched from an api
– ITnyp
Nov 8 at 9:16












I am trying to plot a polyline in my map
– ITnyp
Nov 8 at 9:17




I am trying to plot a polyline in my map
– ITnyp
Nov 8 at 9:17












Ohh, sorry it was a typo edited it
– ITnyp
Nov 8 at 9:18




Ohh, sorry it was a typo edited it
– ITnyp
Nov 8 at 9:18












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










map each item in the outer array, then .map each subarray and split by commas, and map again by Number to transform the strings to numbers:






const input = [
["1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526"],
["1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632"]
];
console.log(
input.map(
subarr => subarr.map(
str => str.split(',').map(Number)
)
)
);








share|improve this answer





















  • I have no idea how to read this, is it possible to store the result into a variable?
    – ITnyp
    Nov 8 at 9:21












  • Just put something like var somevar = in place of console.log(, the console.log in the code above is just to demonstrate that it results in the output you want
    – CertainPerformance
    Nov 8 at 9:23










  • Nice! Exactly how i want it. Thanks again for the help!
    – ITnyp
    Nov 8 at 9:25










  • are you still there? I don't know why but I am return with a different result when I use it on my code
    – ITnyp
    Nov 8 at 9:35










  • The result I am returned is nested in an array
    – ITnyp
    Nov 8 at 9:37


















up vote
0
down vote













A function to do the job:



function splitMyArray(arr) {
for (var i = 0; i < arr.length; ++i) {
var line = arr[i];
for (var k = 0; k < line.length; ++k) {
var parts = line[k].split(',');
line[k] = [parseFloat(parts[0]), parseFloat(parts[1])];
}
}
return arr;
}


Usage example:



var myArr = [
["1.31,103.84", "1.32,103.85"],
["1.39,103.77", "1.40,103.78"]
];

var splitted = splitMyArray(myArr);
console.log(splitted);





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
    });


    }
    });






    ITnyp is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204619%2fangularjs-split-array%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    map each item in the outer array, then .map each subarray and split by commas, and map again by Number to transform the strings to numbers:






    const input = [
    ["1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526"],
    ["1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632"]
    ];
    console.log(
    input.map(
    subarr => subarr.map(
    str => str.split(',').map(Number)
    )
    )
    );








    share|improve this answer





















    • I have no idea how to read this, is it possible to store the result into a variable?
      – ITnyp
      Nov 8 at 9:21












    • Just put something like var somevar = in place of console.log(, the console.log in the code above is just to demonstrate that it results in the output you want
      – CertainPerformance
      Nov 8 at 9:23










    • Nice! Exactly how i want it. Thanks again for the help!
      – ITnyp
      Nov 8 at 9:25










    • are you still there? I don't know why but I am return with a different result when I use it on my code
      – ITnyp
      Nov 8 at 9:35










    • The result I am returned is nested in an array
      – ITnyp
      Nov 8 at 9:37















    up vote
    1
    down vote



    accepted










    map each item in the outer array, then .map each subarray and split by commas, and map again by Number to transform the strings to numbers:






    const input = [
    ["1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526"],
    ["1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632"]
    ];
    console.log(
    input.map(
    subarr => subarr.map(
    str => str.split(',').map(Number)
    )
    )
    );








    share|improve this answer





















    • I have no idea how to read this, is it possible to store the result into a variable?
      – ITnyp
      Nov 8 at 9:21












    • Just put something like var somevar = in place of console.log(, the console.log in the code above is just to demonstrate that it results in the output you want
      – CertainPerformance
      Nov 8 at 9:23










    • Nice! Exactly how i want it. Thanks again for the help!
      – ITnyp
      Nov 8 at 9:25










    • are you still there? I don't know why but I am return with a different result when I use it on my code
      – ITnyp
      Nov 8 at 9:35










    • The result I am returned is nested in an array
      – ITnyp
      Nov 8 at 9:37













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    map each item in the outer array, then .map each subarray and split by commas, and map again by Number to transform the strings to numbers:






    const input = [
    ["1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526"],
    ["1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632"]
    ];
    console.log(
    input.map(
    subarr => subarr.map(
    str => str.split(',').map(Number)
    )
    )
    );








    share|improve this answer












    map each item in the outer array, then .map each subarray and split by commas, and map again by Number to transform the strings to numbers:






    const input = [
    ["1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526"],
    ["1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632"]
    ];
    console.log(
    input.map(
    subarr => subarr.map(
    str => str.split(',').map(Number)
    )
    )
    );








    const input = [
    ["1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526"],
    ["1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632"]
    ];
    console.log(
    input.map(
    subarr => subarr.map(
    str => str.split(',').map(Number)
    )
    )
    );





    const input = [
    ["1.31069258855609,103.848649478524", "1.31138534529796,103.848923050526"],
    ["1.31213221536436,103.848328363879", "1.31288473199114,103.849575392632"]
    ];
    console.log(
    input.map(
    subarr => subarr.map(
    str => str.split(',').map(Number)
    )
    )
    );






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 9:18









    CertainPerformance

    63.1k143051




    63.1k143051












    • I have no idea how to read this, is it possible to store the result into a variable?
      – ITnyp
      Nov 8 at 9:21












    • Just put something like var somevar = in place of console.log(, the console.log in the code above is just to demonstrate that it results in the output you want
      – CertainPerformance
      Nov 8 at 9:23










    • Nice! Exactly how i want it. Thanks again for the help!
      – ITnyp
      Nov 8 at 9:25










    • are you still there? I don't know why but I am return with a different result when I use it on my code
      – ITnyp
      Nov 8 at 9:35










    • The result I am returned is nested in an array
      – ITnyp
      Nov 8 at 9:37


















    • I have no idea how to read this, is it possible to store the result into a variable?
      – ITnyp
      Nov 8 at 9:21












    • Just put something like var somevar = in place of console.log(, the console.log in the code above is just to demonstrate that it results in the output you want
      – CertainPerformance
      Nov 8 at 9:23










    • Nice! Exactly how i want it. Thanks again for the help!
      – ITnyp
      Nov 8 at 9:25










    • are you still there? I don't know why but I am return with a different result when I use it on my code
      – ITnyp
      Nov 8 at 9:35










    • The result I am returned is nested in an array
      – ITnyp
      Nov 8 at 9:37
















    I have no idea how to read this, is it possible to store the result into a variable?
    – ITnyp
    Nov 8 at 9:21






    I have no idea how to read this, is it possible to store the result into a variable?
    – ITnyp
    Nov 8 at 9:21














    Just put something like var somevar = in place of console.log(, the console.log in the code above is just to demonstrate that it results in the output you want
    – CertainPerformance
    Nov 8 at 9:23




    Just put something like var somevar = in place of console.log(, the console.log in the code above is just to demonstrate that it results in the output you want
    – CertainPerformance
    Nov 8 at 9:23












    Nice! Exactly how i want it. Thanks again for the help!
    – ITnyp
    Nov 8 at 9:25




    Nice! Exactly how i want it. Thanks again for the help!
    – ITnyp
    Nov 8 at 9:25












    are you still there? I don't know why but I am return with a different result when I use it on my code
    – ITnyp
    Nov 8 at 9:35




    are you still there? I don't know why but I am return with a different result when I use it on my code
    – ITnyp
    Nov 8 at 9:35












    The result I am returned is nested in an array
    – ITnyp
    Nov 8 at 9:37




    The result I am returned is nested in an array
    – ITnyp
    Nov 8 at 9:37












    up vote
    0
    down vote













    A function to do the job:



    function splitMyArray(arr) {
    for (var i = 0; i < arr.length; ++i) {
    var line = arr[i];
    for (var k = 0; k < line.length; ++k) {
    var parts = line[k].split(',');
    line[k] = [parseFloat(parts[0]), parseFloat(parts[1])];
    }
    }
    return arr;
    }


    Usage example:



    var myArr = [
    ["1.31,103.84", "1.32,103.85"],
    ["1.39,103.77", "1.40,103.78"]
    ];

    var splitted = splitMyArray(myArr);
    console.log(splitted);





    share|improve this answer



























      up vote
      0
      down vote













      A function to do the job:



      function splitMyArray(arr) {
      for (var i = 0; i < arr.length; ++i) {
      var line = arr[i];
      for (var k = 0; k < line.length; ++k) {
      var parts = line[k].split(',');
      line[k] = [parseFloat(parts[0]), parseFloat(parts[1])];
      }
      }
      return arr;
      }


      Usage example:



      var myArr = [
      ["1.31,103.84", "1.32,103.85"],
      ["1.39,103.77", "1.40,103.78"]
      ];

      var splitted = splitMyArray(myArr);
      console.log(splitted);





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        A function to do the job:



        function splitMyArray(arr) {
        for (var i = 0; i < arr.length; ++i) {
        var line = arr[i];
        for (var k = 0; k < line.length; ++k) {
        var parts = line[k].split(',');
        line[k] = [parseFloat(parts[0]), parseFloat(parts[1])];
        }
        }
        return arr;
        }


        Usage example:



        var myArr = [
        ["1.31,103.84", "1.32,103.85"],
        ["1.39,103.77", "1.40,103.78"]
        ];

        var splitted = splitMyArray(myArr);
        console.log(splitted);





        share|improve this answer














        A function to do the job:



        function splitMyArray(arr) {
        for (var i = 0; i < arr.length; ++i) {
        var line = arr[i];
        for (var k = 0; k < line.length; ++k) {
        var parts = line[k].split(',');
        line[k] = [parseFloat(parts[0]), parseFloat(parts[1])];
        }
        }
        return arr;
        }


        Usage example:



        var myArr = [
        ["1.31,103.84", "1.32,103.85"],
        ["1.39,103.77", "1.40,103.78"]
        ];

        var splitted = splitMyArray(myArr);
        console.log(splitted);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 9:33

























        answered Nov 8 at 9:25









        Rodrigo

        998416




        998416






















            ITnyp is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            ITnyp is a new contributor. Be nice, and check out our Code of Conduct.













            ITnyp is a new contributor. Be nice, and check out our Code of Conduct.












            ITnyp is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204619%2fangularjs-split-array%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word