Skip to the content.

$.map(obj, callback)

Translate every entry in an array or plain object into a new flat array.

The callback is invoked with window as this, mirroring jQuery’s static $.map. Returning an array spreads its values into the result; returning null or undefined filters the entry out.

Signatures

map<T, R>(
    obj: Record<string, T> | T[],
    callback: (this: Window, value: T, key: number | string) => R | R[] | null | undefined,
): R[];

Parameters

Returns

A new flat array of the callback’s non-nullish results. Nested arrays are flattened by one level (not recursively).

Examples

import $ from "dabbyjs";
import "dabbyjs/utils/map/map";

// Double every number
const doubled = $.map([1, 2, 3], (n) => n * 2);
// [2, 4, 6]
import $ from "dabbyjs";
import "dabbyjs/utils/map/map";

// Filter out empty strings
const nonEmpty = $.map({ a: "x", b: "", c: "y" }, (v) => v || null);
// ["x", "y"]
import $ from "dabbyjs";
import "dabbyjs/utils/map/map";

// Flatten one level — array returns are spread
const pairs = $.map([1, 2, 3], (n) => [n, n * 10]);
// [1, 10, 2, 20, 3, 30]

Notes

For mapping a Dabby collection of nodes, prefer the instance-level $.fn.map(), which lives on the core class and returns a Dabby collection.

See also