jQuery map()返回对象而不是数组

Tam*_*ara 14 jquery

我有以下代码:

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

我希望结果将是一个数组.但是我得到的对象如下:

Object["55a930cd27daeaa6108b4f68", "55a930cd27daeaa6108b4f67"]
Run Code Online (Sandbox Code Playgroud)

是否有特殊标志传递给函数?在docs中,他们将数组作为返回值.我错过了什么?

jQuery 1.11.2

cha*_*tfl 18

$(selector).map() 总是返回jQuery对象.

从jQuery对象使用获取数组 get()

var selectedTitles = $("#js-otms-titles-table .select_title :checkbox:checked").map(function() {
    return $(this).attr('data-titleId');
}).get();
Run Code Online (Sandbox Code Playgroud)


Sci*_*ter 9

你需要打电话询问.get()最终结果。jQuery 的.map()函数返回一个 jQuery 对象(有时这很方便)。.get()将获取它所构建的底层数组。

请参阅http://api.jquery.com/map/

  • 一种适用于 jQuery 对象(通常是元素),另一种适用于普通对象或数组。 (2认同)

Bob*_*ein 7

设置:

var a = array(1, 2, 3);
var f = function () { return process_each(this); };
var selector;   // e.g. "div.class" or $("div#id1, div#id2, div#id3")
Run Code Online (Sandbox Code Playgroud)

绘制地图的方法*:

$.map(a, f)-数组输入 -数组输出

$(selector).map(f)- jQuery对象输入 - jQuery对象输出

$(selector).map(f).get()- jQuery对象输入 -数组输出(自 jQuery 1.0 起)

$(selector).map(f).toArray()- jQuery对象输入 -数组输出(自 jQuery 1.4 起)

*在此上下文中,映射意味着使用函数一次处理集合中的项目(数组或 jQuery 对象),并输出返回值的集合(数组或 jQuery 对象)。