使用Javascript';s滤波函数
我会尽我最大的努力把这句话说对,谢谢你的帮助 我有一个包含一系列邮政编码的数组(例如六个),其中一个是空的。使用javascripts过滤器函数,我使用以下代码删除了空元素:使用Javascript';s滤波函数,javascript,arrays,filter,Javascript,Arrays,Filter,我会尽我最大的努力把这句话说对,谢谢你的帮助 我有一个包含一系列邮政编码的数组(例如六个),其中一个是空的。使用javascripts过滤器函数,我使用以下代码删除了空元素: var filteredrad = radius1.filter(function(val) { return !(val === "" || typeof val == "undefined" || val === null); }); 现在我需要以某种方式存储从原始数组中删除的元素的索引,但我不确定如何存储 例如
var filteredrad = radius1.filter(function(val) {
return !(val === "" || typeof val == "undefined" || val === null);
});
现在我需要以某种方式存储从原始数组中删除的元素的索引,但我不确定如何存储
例如,筛选器将删除索引1处的空间。我如何保存第一个以备以后使用
["WC1A 1EA", "", "B68 9RT", "WS13 6LR", "BN21TW", "wv6 9ex"]
希望这是有意义的,任何帮助都将不胜感激
Ashley您可以使用一种副作用,使用
过滤器
:
您可以使用
过滤器使用副作用:
filter函数将三个参数传递给回调函数
所以你可以写:
var storedIndexes = []
var filteredrad = radius1.filter(function(val, index) {
val isNull = (val === "" || typeof val == "undefined" || val === null);
if(isNull){
storedIndexes.push(index);
}
return !isNull;
});
并将索引保存在StoredIndex中过滤器函数将三个参数传递给回调函数
radius1 = ["WC1A 1EA", "", "B68 9RT", "WS13 6LR", "BN21TW", "wv6 9ex"];
removed = [];
var filteredrad = radius1.filter(function(val, index) {
if (val === "" || typeof val == "undefined" || val === null) {
removed.push(index);
return false;
}
return true;
});
所以你可以写:
var storedIndexes = []
var filteredrad = radius1.filter(function(val, index) {
val isNull = (val === "" || typeof val == "undefined" || val === null);
if(isNull){
storedIndexes.push(index);
}
return !isNull;
});
并将索引保存在StoredIndex中,这只是另一个以另一种方式实现的示例
radius1 = ["WC1A 1EA", "", "B68 9RT", "WS13 6LR", "BN21TW", "wv6 9ex"];
removed = [];
var filteredrad = radius1.filter(function(val, index) {
if (val === "" || typeof val == "undefined" || val === null) {
removed.push(index);
return false;
}
return true;
});
var collection = ["WC1A 1EA", "", "B68 9RT", "WS13 6LR", "BN21TW", "wv6 9ex"] ;
var postalCodes = (function(){
var emptyIndices;
return {
getCodes: function( array ){
emptyIndices = [];
return array.filter(function( value, index, array ){
if( !value ){
emptyIndices.push(index);
return false;
}else{
return true;
}
});
},
getEmptyIdices: function(){
return emptyIndices || null;
}
};
})();
那就打电话吧
postalCodes.getCodes(collection);
=> ["WC1A 1EA", "B68 9RT", "WS13 6LR", "BN21TW", "wv6 9ex"];
postalCodes.getEmptyIndices();
=> [1];
这只是另一个例子,它以另一种方式实现了你的愿望
var collection = ["WC1A 1EA", "", "B68 9RT", "WS13 6LR", "BN21TW", "wv6 9ex"] ;
var postalCodes = (function(){
var emptyIndices;
return {
getCodes: function( array ){
emptyIndices = [];
return array.filter(function( value, index, array ){
if( !value ){
emptyIndices.push(index);
return false;
}else{
return true;
}
});
},
getEmptyIdices: function(){
return emptyIndices || null;
}
};
})();
那就打电话吧
postalCodes.getCodes(collection);
=> ["WC1A 1EA", "B68 9RT", "WS13 6LR", "BN21TW", "wv6 9ex"];
postalCodes.getEmptyIndices();
=> [1];
尝试添加第二个参数:radius1.filter(函数(val,index)
尝试添加第二个参数:radius1.filter(函数(val,index)
谢谢Jean,这正是我想要的!我会在计时器过期后立即接受它。谢谢Jean,这正是我想要的!我会在计时器过期后立即接受它。我认为当你对你的意图添加一些解释时,这将对OP和后续访问更有帮助。我认为这将对OP和后续访问更有帮助s、 当你在你的意图中添加一些解释时。