Skip to content
jQuery

Utility Methods

Iterate, merge, and filter with jQuery helpers.

By EZ4Code Team
utilitieshelpers

Code

// Iterate over a collection
$("li").each(function (i) {
  console.log(i, $(this).text());
});

// Map to a new array
const texts = $("li").map(function () {
  return $(this).text();
}).get();

// Merge objects
const defaults = { limit: 10, page: 1 };
const opts = $.extend({}, defaults, { limit: 20 });
console.log(opts); // { limit: 20, page: 1 }

// Array helpers
$.grep([1, 2, 3], n => n > 1);     // [2, 3]
$.inArray(2, [1, 2, 3]);            // 1
$.isArray([]);                       // true
$.trim("  hi  ");                    // "hi"

Explanation

each() iterates over jQuery collections with this bound to each DOM element. $.extend merges objects (shallow by default), and $.map/grep provide functional array utilities. These helpers predate modern JS but remain useful for consistency in jQuery codebases.

More jQuery Snippets