0

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:

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?

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

Hope that makes sense, any help would be greatly appreciated.

Ashley

1
  • Try adding a second argument: radius1.filter(function(val, index) Commented Sep 9, 2014 at 12:59

4 Answers 4

5

You can use a side-effect, using filter's second argument:

var removed = [];
var filteredrad = radius1.filter(function(val, index) {
    if (val === "" || typeof val == "undefined" || val === null) {
        removed.push(index);
        return false;
    }
    return true;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jean, exactly what I was after! Will accept it as soon as the timer expires.
0

The filter function passes three arguments to the callback function 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

Comments

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

1 Comment

I think it would be more helpful for the OP and further visitos, when you add some explaination to your intension.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.