in Education by
I store all of the Google Maps marker objects in a single array. Right now I am trying to set up a function that will delete all of the markers in the array from the map, but I'm having trouble with the loop. First, I put each marker in an array like so: eval("markerZip"+value.zip+" = new google.maps.Marker({map: map, icon: '/images/mapmarker.php?m=zip_marker.png', position: zipCenter});"); eval("markersArray['markerZip"+value.zip+"'] = markerZip"+value.zip); Then when I want to delete the markers I do this: function removeAllMarkers(exceptId) { $.each(markersArray, function(index, value) { if(value != exceptId) { value.setMap(null); console.log(value); } }); } However, iterating through the array doesn't seem to be doing anything. It's as if the array is empty because the console.log line returns nothing. When I display the array in my console, it shows "[]" (which I then click to display the child objects) which contains: markerZip01002 U { gm_accessors_={...}, map=U, b=U, more...} markerZip02111 U { gm_accessors_={...}, map=U, b=U, more...} markerZip02135 U { gm_accessors_={...}, map=U, b=U, more...} markerZip02139 U { gm_accessors_={...}, map=U, b=U, more...} markerZip02466 U { gm_accessors_={...}, map=U, b=U, more...} JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The issue is that you are using an array as a hash or dictionary. jQuery checks to see if the object it has been passed has a length property - see here for the implementation. Since you are passing an array, it iterates over the values in the array which can be accessed via the integer keys between 0 and obj.length - 1. Since there are no values in your array that can be accessed that way, the loop immediately terminates. You are treating your markersArray as a JavaScript object rather than an array - so you'd be better off using {} rather than [] for your markersArray object. In addition, there is no need to use eval for setting keys in a hash - ever. It is inefficient - you need to start up a new version of the JavaScript runtime for each invocation of eval. It is fragile - you are writing strings that contain code and injecting other strings inside them - a misplaced " or // in your data can stop your program cold. And finally, it requires continual use of the global scope - which should be avoided whenever you don't own the global scope. You can replace these two lines: eval("markerZip"+value.zip+" = new google.maps.Marker({map: map/*etc.*/});"); eval("markersArray['markerZip"+value.zip+"'] = markerZip"+value.zip); with this one line: // Assume we did this earlier // var markersHash = {}; markersHash["markerZip"+value.zip] = new google.maps.Marker({map: map /*etc.*/}); When you do that, your call to $.each will work as expected.

Related questions

0 votes
    I need to iterate through the elements in a numpy array so I can treat any zero elements separately. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I need to iterate through the elements in a numpy array so I can treat any zero elements separately. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    How to create filled maps in Tableau?...
asked Oct 30, 2020 in Technology by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    i have an array of objects in [ { "country": "USA", "payment": [ { "paymentType": "Visa" ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    How do you think I can get the intersection of the below-given array? a=np.array([[[[0,0],[0,1]],[[1,1],[1,1]]] ... , [[0, 1], [1, 1]]] Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    In multidimensional array mySubject, which of the following will allow assigning the value 100 in position 5 of the third mySubject array? 1 ... ][4] = 100 5. mySubject[3][5] = 100...
asked Feb 24, 2021 in Technology by JackTerrance
+1 vote
    In multidimensional array mySubject, which of the following will allow assigning the value 100 in position 5 of the third mySubject array? a ... 2][4] = 100 e)mySubject[3][5] = 100...
asked Oct 8, 2020 in Technology by JackTerrance
0 votes
    Which is better in terms of performance for iterating an array? (a) for(int i=0; i=0; i-) (c) ... Autoboxing & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which is better in terms of performance for iterating an array? (a) for(int i=0; i=0; i-) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    I am trying to create an app using AngularJS that takes a user's location (from their mobile) and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I am trying to create an app using AngularJS that takes a user's location (from their mobile) and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
...