Flatten one level
A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.
The task
Return a new array where items that are arrays are replaced by their contents, one level deep only. Items that are not arrays (numbers, strings, `null`, objects) stay as they are, and deeper arrays stay nested.
What this exercise teaches
Array.isArray is the reliable test: typeof says "object" for arrays, plain objects and null alike, so it would try to spread null. flat() with no argument flattens exactly one level.
A hint if you get stuck
Check each item with `Array.isArray`. Spread arrays into the result and push everything else. The built-in `arr.flat()` does exactly one level too.
This exercise is part of Pro
Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.
You create a free account first, so Pro is attached to it.
Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.