Array.prototype.concat
JS Functions
Easy
15 min
arrays
polyfills
symbols
Implement Array.prototype.myConcat(...items), a polyfill for the native Array.prototype.concat. It returns a new array made of this followed by each argument. Arguments that are arrays are spread one level deep. Any other value is appended as a single element.
The starter attaches the function to Array.prototype and default-exports it.
[1, 2].myConcat([3, 4], 5); // [1, 2, 3, 4, 5]
[1].myConcat([[2]], 'ab'); // [1, [2], 'ab'] (only one level, strings are not spread)
[1, , 3].myConcat([4]); // [1, <hole>, 3, 4] (holes are kept)
const copy = [1, 2].myConcat(); // a new array, copy !== original
Requirements
- Never mutate
thisor the arguments. - Spread a value only if it is "concat-spreadable":
- If the value is an object with a
Symbol.isConcatSpreadableproperty that is notundefined, spread it when that property is truthy. - Otherwise, spread it only if
Array.isArray(value)is true. Plain array-like objects such as{ length: 2, 0: 'a', 1: 'b' }are appended as-is.
- If the value is an object with a
- The same rule applies to
this, so the receiver itself might be appended as one element. - Preserve holes: if index
kis missing from a spread source, the matching index is missing from the result too, and the result'slengthstill accounts for it. - Do not use the native
concat, spread syntax on the sources, orflat.
Follow-up
- How does
concatdiffer from[...a, ...b]? - Why might a library set
Symbol.isConcatSpreadableon a custom collection class?
Spotted a mistake or a rights concern? Report an issue