4

Why would the following jquery code sometimes throw the error "concat is not a function":

var myArray = $('div.foo')
    .filter(function() { return $(this).is('.something'); })
    .map(function() { 
        return [['a', 'b', $(this).val()]];
    });

return myArray.concat(anotherArray);
cbp
  • 25,252
  • 29
  • 125
  • 205

2 Answers2

7

$().map() returns a jQuery object, not an array.
jQuery objects do not have a concat() method.

You need to call .get() to get a real array.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Ah ok, the jquery documentation is annoying in this regard: "The $.map() method applies a function to each item in an array or object and maps the results into a new array." – cbp Sep 20 '12 at 02:21
  • Yes but in this case, you're operating on a jQuery object, not an array. `$.map(myArray, function(){})` is not the same as `$('selector').map(function(){})` – BLSully Sep 20 '12 at 02:23
1

contact is for ARRAYS, and not for OBJECTS. Use:
1) Filter Form Parameters Before Submitting
Or
2) How can I merge properties of two JavaScript objects dynamically?

Community
  • 1
  • 1
T.Todua
  • 53,146
  • 19
  • 236
  • 237