Hello,
You can use indexOf
to identify the position where your value (city id) is.
After this, you can remove the item with splice
. It tells you the position where you want to start taking the removal, and the number of items you want to remove from that (1 in your case, I believe). In addition, the return of splice
is the new array with the remotions made.
It would look something like this:
function onRemove(city) {
var posicao = array.indexOf(city);
array.splice(posicao, 1);
}
Note: browser support for indexOf
is limited. Not supported in Internet Explorer 7 and 8.
Of course, if indexOf
does not work very well for you, you can find the city in question the way you did (sweeping with for). However, I believe splice
is useful.