是否有像Ruby一样的jQuery(&:map)函数?

Lan*_*ard 2 arrays jquery

我想从一系列项目中获取所有ID,我怎样才能做出这一条短线:

var ids = [];
$(".post").each(function(index, element) {
  ids.push($(element).attr("id"));
});
Run Code Online (Sandbox Code Playgroud)

就像是:

var ids = $(".post").map("id");
Run Code Online (Sandbox Code Playgroud)

gna*_*arf 8

对! .map()用于jQuery对象,或$.map用于数组和对象.jQuery版本将返回一个应用了map函数的jQuery对象,因此你必须调用.get()以获取实际的数组.

var ids = $(".post").map(function(index, element) { return element.id }).get();
Run Code Online (Sandbox Code Playgroud)