JavaScript copying object arrays -
JavaScript copying object arrays -
i tried copying array of objects , failed terribly. every time ended array of references original aray.
i tried ".concat()", used "for" copying every element separately, every time made alter in temporary array, original array changed too.
here's code.
glossary: tablicaznacznikow - original array placeholder - temporary array tempclosest - id of closest marker startingpointid - id of marker witch start calculation .meta field - defines if marker has been added polyline var placeholder = tablicaznacznikow.concat(); var temparrayofsomething = [placeholder[startingpointid].latlng]; (var = 0; < placeholder.length; i++) { var tempclosest = findclosestmarkertoanothermarker(placeholder, startingpointid); temparrayofsomething.push(placeholder[tempclosest].latlng); startingpointid = tempclosest; placeholder[tempclosest].meta = "used"; console.log(tempclosest); }
i utilize code create array making path gmap3 polyline. in advance.
in order clone array of objects can utilize map
method , homecoming object re-create in each iteration. convenient making object re-create simple $.extend
. together:
var newarr = tablicaznacznikow.map(function(el) { homecoming $.extend({}, el); });
or shorter:
var newarr = tablicaznacznikow.map($.extend.bind(null, {}));
that's it. map
creates new array, , $.extend
clones objects. clone resulting array of objects.
note: used 2 es5 methods supported ie9+. if back upwards older ie's utilize $.map
instead of array.prototype.map
, $.proxy
instead of array.prototype.bind
.
javascript arrays javascript-objects jquery-gmap3
Comments
Post a Comment