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

时间:2016-03-01 20:32:17

标签: 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);

我希望结果是一个数组。但是我收到的对象如下:

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

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

jQuery 1.11.2

3 个答案:

答案 0 :(得分:12)

$data总是返回jQuery对象。

要从jQuery对象获取数组,请使用getData()

$(selector).map()

答案 1 :(得分:2)

您需要在最终结果上调用.get()。 jQuery的.map()函数返回一个jQuery对象(有时可以很方便)。 .get()将获取它构建的基础数组。

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

答案 2 :(得分:1)

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

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

$(selector).map(f) - jQuery对象in -jQuery object out

$(selector).map(f).get() - jQuery对象in-array out(自jQuery 1.0起)

$(selector).map(f).toArray() - jQuery对象in-array out(自jQuery 1.4起)

相关问题