Reputation: 235
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?
Upvotes: 1
Views: 163
Reputation: 17640
You can use and alias array and loop through them
var options_array = ["motorbike",'bicycle','bike','car','vanagon'];
function remove_option(option,alias_array) {
var option_index = options_array.indexOf(option);
if(option_index != -1){
if(alias_array.indexOf(option) != -1){
for (var i=0;i<alias_array.length;i++){
alias_index = options_array.indexOf(alias_array[i]);
if(alias_index != -1){
options_array.splice(alias_index, 1);
}
}
}
}
return options_array;
}
console.log(remove_option('bike',['motorbike','bicycle','bike']));
Upvotes: 0
Reputation: 17815
Array.filter
needs a ECMAScript 5th edition compilant environment.options_array
valuevar options_array = [ 'foo', 'bar', 'baz', 'bicycle', 'motorbike' ];
options_array = options_array.filter(function(value){
return value == 'bicycle';
});
// options_array is now [ 'foo', 'bar', 'baz', 'motorbike' ]
var options_array = [ 'foo', 'bar', 'baz', 'bicycle', 'motorbike' ];
function remove_option( option_name ){
options_array = options_array.filter(function(value){
return value == 'bicycle';
});
}
remove_option( 'bicycle' );
// options_array is now [ 'foo', 'bar', 'baz', 'motorbike' ]
remove_option( 'foo' );
// options_array is now [ 'bar', 'baz', 'motorbike' ]
var options_array = [ 'foo', 'bar', 'baz', 'bicycle', 'motorbike' ];
var options_array2 = [ 'foo', 'bar', 'baz' ];
function remove_option( options_array, option_name ){
options_array = options_array.filter(function(value){
return value == 'bicycle';
});
}
remove_option( options_array, 'bicycle' );
// options_array is now [ 'foo', 'bar', 'baz', 'motorbike' ]
remove_option( options_array2, 'foo' );
// options_array2 is now [ 'bar', 'baz', 'bicycle', 'motorbike' ]
You could even build an object prototype to manage option arrays if its need, but i think the examples are enought.
Upvotes: 1
Reputation: 48415
How about using an array of synonyms for each word:
var synonyms = [
['bicycle', 'motorbike'],
['car', 'van']
];
Then you can get the matching list with a helper function:
//this will return an array of related words (e.g. ['bicycle', 'motorbike'])
function getSynonyms(item) {
for (var i = 0; i < synonyms.length; i++) {
var arr = synonyms[i];
for (var j = 0; j < arr.length; j++) {
if (arr[j] == item) return arr;
}
}
return null;
}
and finally your remove function can be changed like so:
//this will remove the option plus any matching synonym words
function remove_option(option) {
var synonymsToRemove = getSynonyms(option);
//check if any matches found, if not then just used the single specified option
if (!synonymsToRemove) synonymsToRemove = [option];
for (var i = 0; i < synonymsToRemove.length; i++) {
var option_index = options_array.indexOf(synonymsToRemove[i]);
if (option_index != -1) {
options_array.splice(option_index, 1);
}
}
}
Which you can use like so:
console.log(options_array);//["bicycle", "motorbike", "car", "van"]
remove_option('bicycle');//remove 'bicycle' and related words (i.e. 'motorbike')
console.log(options_array);//["car", "van"]
Upvotes: 0
Reputation: 388316
Try
options_array = []
//this can be enhanced to support multiple elements like 'motorbike, tricycle'
var related = {
'bicycle': 'motorbike',
'motorbike': 'bicycle'
}
function remove_option(option) {
remove_opts(option)
var relation = related[option];
if (relation) {
remove_opts(relation)
}
}
function remove_opts(option){
var option_index = options_array.indexOf(option);
if (option_index != -1) options_array.splice(option_index, 1);
}
Upvotes: 0