我正在尝试用javascript编写函数来从数组中删除字符串。数组是动态创建的,我不时会向其中添加新的字符串。当我从中删除字符串时,我首先检查字符串是否真的在其中。
我有一个额外的条件。如果要移除的字符串等于“自行车”,那么我也要检查“摩托车”,如果存在则将其删除。如果给出的字符串是“摩托车”,我想删除可能出现的“自行车”。
到目前为止,我有这样的事情:
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);
}
}
它有效,但有可能使它更好更干吗?
答案 0 :(得分:1)
Array.filter
需要ECMAScript第5版兼容环境。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' ]
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' ]
如果需要的话,你甚至可以构建一个对象原型来管理选项数组,但我认为这些例子都是适用的。
答案 1 :(得分: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);
}
答案 2 :(得分: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']));
答案 3 :(得分: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"]