Skip to main content
added 244 characters in body
Source Link
Sean Gugler
  • 779
  • 8
  • 19

The .map() function in jQuery returns a jQuery object:

var $collection = $(selector).map(extractSomeValue) 

Most often, what is desired is a plain array of the mapped values:

var extractedArray = $collection.get() 

In fact, ALL the use cases and examples I have found always pair .map() with .get(), including jQuery's own documentation.

What is the use case for working with the $collection as is? Why doesn't .map() simply return the array if that's what everyone uses it for?

EDIT: To be clear, I am referring only to jQuery.fn.map, not jQuery.map (aka $.map), since the latter is well-defined to operate only on non-jQuery objects and sensibly return a plain array.

The .map() function in jQuery returns a jQuery object:

var $collection = $(selector).map(extractSomeValue) 

Most often, what is desired is a plain array of the mapped values:

var extractedArray = $collection.get() 

In fact, ALL the use cases and examples I have found always pair .map() with .get(), including jQuery's own documentation.

What is the use case for working with the $collection as is? Why doesn't .map() simply return the array if that's what everyone uses it for?

The .map() function in jQuery returns a jQuery object:

var $collection = $(selector).map(extractSomeValue) 

Most often, what is desired is a plain array of the mapped values:

var extractedArray = $collection.get() 

In fact, ALL the use cases and examples I have found always pair .map() with .get(), including jQuery's own documentation.

What is the use case for working with the $collection as is? Why doesn't .map() simply return the array if that's what everyone uses it for?

EDIT: To be clear, I am referring only to jQuery.fn.map, not jQuery.map (aka $.map), since the latter is well-defined to operate only on non-jQuery objects and sensibly return a plain array.

Source Link
Sean Gugler
  • 779
  • 8
  • 19

When is jQuery .map useful without .get?

The .map() function in jQuery returns a jQuery object:

var $collection = $(selector).map(extractSomeValue) 

Most often, what is desired is a plain array of the mapped values:

var extractedArray = $collection.get() 

In fact, ALL the use cases and examples I have found always pair .map() with .get(), including jQuery's own documentation.

What is the use case for working with the $collection as is? Why doesn't .map() simply return the array if that's what everyone uses it for?