JQuery map () returns an object instead of an array

I have the following code:

var selectedTitles = $("#js-otms-titles-table .select_title :checkbox:checked").map(function() { return $(this).attr('data-titleId'); }); console.log("Selected titles"); console.log(selectedTitles); 

I expect the result to be an array. However, I get an object like:

 Object["55a930cd27daeaa6108b4f68", "55a930cd27daeaa6108b4f67"] 

Is there a special flag for passing a function? In docs, they talk about arrays as a return value. Did I miss something?

jQuery 1.11.2

+5
source share
3 answers

$(selector).map() always returns a jQuery object.

To get an array from a jQuery object, use get()

 var selectedTitles = $("#js-otms-titles-table .select_title :checkbox:checked").map(function() { return $(this).attr('data-titleId'); }).get(); 
+5
source

You need to call .get() in the final result. The jQuery .map() function returns a jQuery object (which can sometimes be convenient). .get() will retrieve the underlying array on which it was built.

See http://api.jquery.com/map/

+1
source
 var a = array(1, 2, 3); var f = function () { return do_something_with_each(this); }; 

$.map(a, f) - an array in an array

$(selector).map(f) - jQuery object in jQuery object

$(selector).map(f).get() - jQuery in-array out object (since jQuery 1.0)

$(selector).map(f).toArray() - jQuery in-array out object (since jQuery 1.4)

0
source

Source: https://habr.com/ru/post/1244251/


All Articles