-1

I have an array called current markers, and each time a function is called the marker is added to the array

var currentMarkers=[];
          
        var mymarker=     new mapboxgl.Marker(el)
          .setLngLat(marker.geometry.coordinates
          )
          .addTo(map); 
          
this.currentMarkers.push(mymarker); // pushing into the array

Is there any other method out there so that i can just update the values of the array instead of pushing new values into it? example

just my concept

this.currentMarkers[0].update(mymarker);    

Thank you so much for your time !

2
  • 4
    this.currentMarkers[0] = mymarker? Commented Jun 30, 2021 at 0:16
  • 1
    you should use push for adding items to an array. if you want to come up with something else, thats going to hurt maintainability - especially for others reading your code. Commented Jun 30, 2021 at 0:17

1 Answer 1

4

If you want to update a specific element of an array in JavaScript, just reference that element and assign it.

Example:

this.currentMarkers[0] = mymarker;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.