Hello,
You can use the indexOf
to identify the position where its value (city id) is.
After that, you can remove the item with splice
. In it you indicate the position where you want to start removing, 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 removals done.
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 the indexOf
does not work very well for you, it is possible to locate the city in question the way you did (sweeping with the for). However, I believe that the splice
be helpful.
Don’t put the solution in the question. That’s not how Stackoverflow works. If @Ricardopunctual provided the solution you wanted then mark it as right already indicates it is solution. Otherwise and if you think it is something different, you can even answer your question with what you consider solution.
– Isac
Perfect! Thank you!
– Bruno Cabral