jQuery的地图()返回对象,而不是阵列
问题描述:
我有以下代码: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
答
$(selector).map()
总是返回jQuery对象。
要想从jQuery对象数组使用get()
var selectedTitles = $("#js-otms-titles-table .select_title :checkbox:checked").map(function() {
return $(this).attr('data-titleId');
}).get();
答
你需要调用.get()
对最终结果。 jQuery的.map()
函数返回一个jQuery对象(有时可能很方便)。 .get()
将获取它所构建的底层数组。
答
var a = array(1, 2, 3);
var f = function() { return do_something_with_each(this); };
$.map(a, f)
- 阵列 - 排列出
$(selector).map(f)
- 在jQuery对象 - jQuery对象了
$(selector).map(f).get()
- jQuery对象in - array out(自jQuery 1.0以来)
$(selector).map(f).toArray()
- jQuery对象in-array out(自jQuery 1.4以来)
http://api.jquery.com/map/和http://api.jquery.com/jquery.map/有什么区别? ?我有点困惑。 – Tamara
一个在jQuery对象(通常是元素)上工作,另一个在普通对象或数组上。 – Scimonster