javascript - Multidimensional array push -
how can this?
"type" = string
showmarkers('vehicles'); showmarkers('houses'); hidemarkers('vehicles'); var my_array = []; function showmarkers(type) { //code var gmm = new google.maps.marker( { //code }); my_array[type].push(gmm); //uncaught typeerror: cannot call method 'push' of undefined } function hidemarkers(type) { for(var = 0; < my_array[type].length; i++) { my_array[type][i].setmap(null); //hide markers } my_array[type] = []; //empty array of `type` }
i got 3 different map icons want users able remove individually
th problem my_array[type]
undefined if call showmarkers
before hidemarkers
.
change function this:
function showmarkers(type) { //code var gmm = new google.maps.marker( { //code }); if (!(my_array[type] instanceof array)) { my_array[type] = []; } my_array[type].push(gmm); }
Comments
Post a Comment