简体   繁体   English

使用Javascript的过滤功能

[英]Using Javascript's filter function

I will do my best to word this correctly - thanks for any help. 我将竭尽所能正确地说出这句话-感谢您的帮助。

I have an array with a series of postcodes in them, (six for example) one of which will be empty. 我有一个数组,其中包含一系列邮政编码,(例如六个),其中一个将为空。 Using javascripts filter function, I removed the element that was empty using the following code: 使用javascripts过滤器功能,我使用以下代码删除了为空的元素:

var filteredrad = radius1.filter(function(val) {
  return !(val === "" || typeof val == "undefined" || val === null);
});

Now I need to somehow store the index of which element(s) were removed from the original array, but im unsure on how to. 现在,我需要以某种方式存储从原始数组中删除了哪些元素的索引,但是不确定如何存储。

As an example, the filter would remove the space at index 1. How do I save that number one for use later on? 例如,过滤器将删除索引1处的空间。如何保存该数字以备后用?

["WC1A 1EA", "", "B68 9RT", "WS13 6LR", "BN21TW", "wv6 9ex"] 

Hope that makes sense, any help would be greatly appreciated. 希望这是有道理的,任何帮助将不胜感激。

Ashley 阿什利

You can use a side-effect, using filter 's second argument : 您可以使用filter第二个参数来产生副作用:

var removed = [];
var filteredrad = radius1.filter(function(val, index) {
    if (val === "" || typeof val == "undefined" || val === null) {
        removed.push(index);
        return false;
    }
    return true;
});

The filter function passes three arguments to the callback function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter filter函数将三个参数传递给回调函数https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

So you could write: 所以你可以这样写:

var storedIndexes = []
var filteredrad = radius1.filter(function(val, index) {
  val isNull = (val === "" || typeof val == "undefined" || val === null);
  if(isNull){
    storedIndexes.push(index);
  }
  return !isNull;
});

And have the indexes saved in storedIndexes 并将索引保​​存在storedIndexes中

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

Just another example that does what you wish in another way 另一个例子以另一种方式实现了您的期望

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

Then just call 然后打电话

postalCodes.getCodes(collection);
=> ["WC1A 1EA", "B68 9RT", "WS13 6LR", "BN21TW", "wv6 9ex"];

postalCodes.getEmptyIndices();
=> [1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM