I'm trying to write function in javascript for removing strings from array. Array is dynamically created and from time to time I add new strings to it. When I remove strings from it I first check if string is really in it.
I have one additional condition. If string that is gonna be removed is equal for example to 'bicycle' than I want to also check for 'motorbike' and if it exists remove it also. And if string given is 'motorbike' I want to remove possible occurences of 'bicycle'.
So far I have something like this:
options_array = []
function remove_option(option) {
var option_index = options_array.indexOf(option);
if(option_index != -1)
options_array.splice(option_index, 1);
if(option == 'bicycle') {
option_index = options_array.indexOf('motorbike');
if(option_index != -1)
options_array.splice(option_index, 1);
} else if(option == 'motorbike') {
option_index = options_array.indexOf('bicycle');
if(option_index != -1)
options_array.splice(option_index, 1);
}
}
It works but is it possible to make it nicer and more DRY?