PHP array_map() Equivalent in JavaScript
JavaScript's Array.prototype.map() is a near-exact match for PHP's array_map() on plain lists, but PHP's key-preserving and multi-array behavior have no direct one-line equivalent.
Close equivalent Covers the common case, but has documented behavioral differences.
- PHP source
array_map()- Closest JavaScript API
Array.prototype.map()
Main limitation: JavaScript `Array.prototype.map()` is eager (returns a new array immediately), matching PHP's behavior — unlike Python's lazy `map()`.
How to use each function
Before comparing the two, here's how array_map() and Array.prototype.map() work
on their own.
PHP: array_map()
array_map(callable $callback, array $array, array ...$arrays): array
Applies a callback to every element of one or more arrays and returns a new array of the results. When multiple arrays are passed, the callback receives one element from each array per call, and PHP pads any shorter array with null.
Returns: A new array with the same length as the longest input array.
$prices = [10, 20, 30];
$withTax = array_map(fn($p) => $p * 1.2, $prices);
// [12, 24, 36] JavaScript: Array.prototype.map()
arr.map(callbackFn(element, index, array), thisArg)
Calls callbackFn once for every element and returns a new array of the results, immediately and in order — eager, like PHP's array_map(), unlike Python's lazy map(). It only iterates a single array natively; combining a second array means reading it by index inside the callback.
Returns: A new array of the same length as the original.
[10, 20, 30].map((n) => n * 1.2);
// [12, 24, 36] Quick mapping
| PHP | array_map() |
|---|---|
| Closest JavaScript API | Array.prototype.map() |
How it differs
- JavaScript
Array.prototype.map()is eager (returns a new array immediately), matching PHP's behavior — unlike Python's lazymap(). - PHP's
array_map()preserves string keys on a single associative array; JavaScript arrays don't have string keys in the same sense, so converting an object's values requiresObject.values(obj).map(...)orObject.fromEntries()if you need to keep the keys. - PHP
array_map()accepts multiple arrays and passes corresponding elements to the callback as separate arguments; JavaScript'smap()only iterates a single array — combine arrays first with something like index-based access, or restructure the data.
Examples
Doubling every element
$doubled = array_map(fn($n) => $n * 2, [1, 2, 3]);
// [2, 4, 6] const doubled = [1, 2, 3].map((n) => n * 2);
// [2, 4, 6] Multiple source arrays
array_map(fn($a, $b) => $a + $b, [1, 2], [10, 20]);
// [11, 22] [1, 2].map((a, i) => a + [10, 20][i]);
// [11, 22] Edge cases
Edge case
JavaScript Array.prototype.map() always returns an array of the same length as the input, including for sparse arrays (it visits holes but the callback still runs for indices that exist) — verify behavior with sparse arrays if your PHP code assumes reindexing.
References
- PHP Manual — array_map — checked 2026-07-29
- MDN Web Docs — Array.prototype.map() — checked 2026-07-29
Last verified 2026-07-29.