javascript - Create an array of objects from fields on a page -
i have page displays hotel data in list. have data on page:
<input type="hidden" class="name" value="hotel1" /> <input type="hidden" class="latitude" value="-12.3456" /> <input type="hidden" class="longitude" value="12.3456" /> <input type="hidden" class="name" value="hotel2" /> <input type="hidden" class="latitude" value="98.7654" /> <input type="hidden" class="longitude" value="-98.7654" />
i'd create array of objects values, can loop through each hotel , this:
$.each(hotels, function (index, obj) { var name = obj.name; var lat = obj.latitude; var long = obj.longitude; });
you might need change how select dom elements, more-or-less right thing:
var $names = $('input[type="hidden"].name'), hotels = $names.map(function () { var $this = $(this), $lat = $this.next(), $lng = $lat.next(); return { name: $this.val(), lat: $lat.val(), lng: $lng.val() }; }).get();
i used lng
instead of long
since long
reserved word in many programming languages (though no, not in js).
Comments
Post a Comment