1

我正在尝试用 javascript 编写函数来从数组中删除字符串。数组是动态创建的,我不时向它添加新字符串。当我从中删除字符串时,我首先检查字符串是否真的在其中。

我还有一个附加条件。如果要删除的字符串等于“bicycle”,那么我还想检查“motorbike”,如果存在也将其删除。如果给定的字符串是“摩托车”,我想删除可能出现的“自行车”。

到目前为止,我有这样的事情:

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);
  }
}

它可以工作,但有没有可能让它变得更好、更干燥?

4

4 回答 4

1

请参阅Mozilla 开发人员网络上的 Array.prototype.filter 参考

请注意,Array.filter需要 ECMAScript 第 5 版编译环境。

示例实现

1.重新评估options_array价值

var 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' ]

2. 与示例#1 相同的可重用函数

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' ]

3. 与示例 #2 相同的可重用函数,但使用任何数组

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' ]

如果需要,您甚至可以构建一个对象原型来管理选项数组,但我认为这些示例已经足够了。

于 2013-10-08T14:25:51.783 回答
0

尝试

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);
}
于 2013-10-08T13:45:06.790 回答
0

您可以使用和别名数组并遍历它们

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']));
于 2013-10-08T13:49:54.017 回答
0

如何为每个单词使用一组同义词:

var synonyms = [
    ['bicycle', 'motorbike'],
    ['car', 'van']
];

然后您可以使用辅助函数获取匹配列表:

//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;
}

最后你的删除功能可以这样改变:

//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);
        }
    }
}

您可以像这样使用它:

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"]

这是一个工作示例

于 2013-10-08T13:51:13.133 回答