javascript - Transform array attribute in AngularJS -


i have plain array in javascript:

$scope.myarray = [{ "coords" : { "lat" : 0, "long" : 0 } }, { "coords" : { "lat" : 1, "long" : 1 } }, { "coords" : { "lat" : 2, "long" : 2 } }]; 

then, have angular-google-maps module, draw these points paths screen.

<ui-gmap-google-map center='map.center' zoom='map.zoom'>     <ui-gmap-polyline path="myarray" stroke="p.stroke" visible='p.visible' geodesic='p.geodesic' fit="false" editable="p.editable" draggable="p.draggable" icons='p.icons'>     </ui-gmap-polyline> </ui-gmap-google-map> 

this directive, however, expects path array of positions, this:

$scope.myarray = [{ "lat" : 0, "long" : 0 }, { "lat" : 1, "long" : 1 }, { "lat" : 2, "long" : 2 }]; 

is there way transform between first array 1 ui-gmap-polyline expects? thinking writing filter, i'm not sure if that's best way. positions added (and possibly changed) myarray, , want google map update along this. way watch myarray changes , create new array every time feed map, there seems simple solution i'm missing.

e.g.: path="myarray | filter:coords" -> there way filter array return fields of each element?

edit: myarray synchronised $firebasearray (https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray). array updated firebase api automatically when backend updated (out of control).

you can use map

arr.map(function(e) { return e.coords; }); 

demo

var arr = [{    "coords": {      "lat": 0,      "long": 0    }  }, {    "coords": {      "lat": 1,      "long": 1    }  }, {    "coords": {      "lat": 2,      "long": 2    }  }];    arr = arr.map(function(e) {    return e.coords;  });    console.log(arr);  document.getelementbyid('output').innerhtml = json.stringify(arr, 0, 2);
<pre id="output"></pre>


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -