7

I have a jQuery object obj that encapsulates a set of input elements.

I am using this code in order to make an array out of each element's value :

$.map(obj, function(elem, i){
    return $(elem).val();
});

The problem is that sometimes some of the input fields contain invalid values and i would like to skip them in the process of creating the array. Doing a simple return false doesn't seem to skip the element but instead inserts a false in the array.

I would like to know if there is a way to actually do that without using .each explicitly.

Dany Khalife
  • 1,850
  • 3
  • 20
  • 47

2 Answers2

12

Since it's an jQuery collection, you can execute .map directly and then use .get so that it becomes an array.

var values = obj.map(function(){
    return this.value ? this.value : null;
}).get();

Note: The above check will return any value that isn't falsey.

See test case on jsFiddle.

mekwall
  • 28,614
  • 6
  • 75
  • 77
  • 1
    We could do it in this way also `var myArray = ['one', 'two', 'three']; var i = myArray.filter(function(n){ return n });` May i know y did you use `get` ? – wuhcwdc Jun 16 '13 at 21:20
  • @PankajGarg If you have an array to begin with, sure. In this case the OP has a jQuery collection and want an array with valid values. Using `.map` in conjunction with `.get` will first map our jQuery collection and then convert it into an array. Note that jQuery's `.filter` and `.map` works differently compared to the similar methods in the native `Array` object. – mekwall Jun 16 '13 at 21:30
5

I should have tried returning nothing (void) that did the trick, don't know why this didn't come across my head earlier.

In case you want to do the same, here is the fix:

$.map(obj, function(elem, i){
    if($(elem).val().length == 0) return;

    return $(elem).val();
});

EDIT

I just tested the length here and omitted to the validation code in order stay on the same subject

Dany Khalife
  • 1,850
  • 3
  • 20
  • 47
  • This seems incorrect, There is a difference between **invalid values** and blank/null? – wuhcwdc Jun 16 '13 at 20:30
  • I suggest that `return;` obtains `undefined`. That is, results in undefined rather than specifically returning 'undefined'. This causes the element not to be inserted in the array. However, I can't substantiate this without research (or other comments). Mozilla state that undefined is _returned_. Best not to pursue this too far - I think this discussion has taken place many times before ;) – Andy G Jun 16 '13 at 20:34
  • @PankajGarg i omitted the whole validation code on purpose just to stay on subject... – Dany Khalife Jun 16 '13 at 20:51
  • @DanyKhalife Checking the length is good (I tend to do this often) if you also want to discard an empty string ''. – Andy G Jun 16 '13 at 20:56
  • 2
    @AndrewGibson This is not the same as the native `Array.map`. Returning `null` or `undefined` will not insert anything to the array, according to [the docs](http://api.jquery.com/jQuery.map/). – mekwall Jun 16 '13 at 20:59