Search code examples
javascriptjqueryunderscore.jslodashlinq.js

how to convert array of arrays to array of objects


I am trying to create a json file to test out some react-native map polygon functionality. I have geoJson available from a project using leaflet maps. I need to format the lat/lng points. I have drilled down from the top level-geometry-coordinates. but I am stuck on what to do next. Since i only need the end result for testing any library can be used in the plunker to get the desired result.

here is where i am at.

[
[
    [
        -106.75845,
        34.659846
    ],
    [
        -106.81188,
        34.649485
    ],
    [
        -106.80648,
        34.646378
    ],
    [
        -106.75845,
        34.659846
    ]
],
[
    [
        -106.70432,
        34.650473
    ],
    [
        -106.79663,
        34.720663
    ],
    [
        -106.7278,
        34.637498
    ],
    [
        -106.70432,
        34.650473
    ]
]

]

this is the what i need the end result to look like. plunker

  // desired result
  var result = [[{
                    latitude: 0,
                    longitude: 0
                }, {
                    latitude: 0,
                    longitude: 0
                }, {
                    latitude: 0,
                    longitude: 0
                }], [{
                    latitude: 0,
                    longitude: 0
                }, {
                    latitude: 0,
                    longitude: 0
                }, {
                    latitude: 0,
                    longitude: 0
                }]];

Solution

  • try this updated plunker

    obj = obj.map(function(innerArray){
       return innerArray.map(function(value){ 
          return {latitude:value[0], longitude:value[1]}  ;
       });
    });
    console.log(obj);