Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Think of flat() as unpacking nested boxes! When you have arrays inside arrays (like [1, [2, 3], [4, [5]]]), flat() unpacks them into one single-level array. And flatMap() is like doing map() then flat() in one go - super efficient!
arr.flat() unpacks one level
flat(2) unpacks 2 levels deep
flat(Infinity) unpacks all levels!
flatMap() runs a function on each element (like map), then flattens the result by 1 level. More efficient than doing both separately!
arr.flatMap(x => [x * 2]) // Map then flattenUnpack arrays at different depths
Map and flatten in one efficient step
flat() removes array nesting levels
Pass number to control how many levels to unpack
More efficient than map().flat()
Use flat(Infinity) for any depth