Table of Contents
What flat() Does
- Flattens nested arrays: it pulls the items out of the inner arrays and places them in one list.
- Returns a new array: the result is separate from the original.
- Leaves the original array alone: nothing in it is changed.
- Goes one level deep by default: you can tell it to go deeper.
let nested = [1, 2, [3, 4], 5]; let flat = nested.flat(); console.log(flat); // [1, 2, 3, 4, 5] console.log(nested); // [1, 2, [3, 4], 5], unchanged
Syntax and Parameters
array.flat(depth);
depth: how many levels of nesting to flatten. This is optional, and it defaults to 1.
Infinity: flattens every level, no matter how deep the nesting goes.
let deep = [1, [2, [3, [4]]]]; console.log(deep.flat()); // [1, 2, [3, [4]]] console.log(deep.flat(2)); // [1, 2, 3, [4]] console.log(deep.flat(Infinity)); // [1, 2, 3, 4]
Practical Examples
Combining grouped data into one list:let teams = [["Vi", "Sam"], ["Alex"], ["Priya", "Rohan"]]; let everyone = teams.flat(); console.log(everyone); // ["Vi", "Sam", "Alex", "Priya", "Rohan"]Working with a grid:
let grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; console.log(grid.flat()); // [1, 2, 3, 4, 5, 6, 7, 8, 9]Finding the largest value in a grid:
console.log(Math.max(...grid.flat())); // 9Collecting tags from several posts:
let posts = [
{ title: "Post 1", tags: ["js", "css"] },
{ title: "Post 2", tags: ["html"] },
{ title: "Post 3", tags: ["js", "react"] }
];
let allTags = posts.map(post => post.tags).flat();
console.log(allTags); // ["js", "css", "html", "js", "react"]
Removing duplicates after flattening:
console.log([...new Set(allTags)]); // ["js", "css", "html", "react"]
It Removes Empty Slots
Empty slots are dropped: an array with gaps comes back without them.let gaps = [1, , 3, , 5]; console.log(gaps.flat()); // [1, 3, 5]This only applies to empty slots: values such as undefined and null are kept.
console.log([1, undefined, null, 4].flat()); // [1, undefined, null, 4]
flatMap(): Map and Flatten Together
flatMap() combines map() and flat(): it transforms each item and then flattens the result by one level.
let sentences = ["hello world", "good morning"];
let words = sentences.flatMap(sentence => sentence.split(" "));
console.log(words); // ["hello", "world", "good", "morning"]
Doing the same with two steps:
let sameResult = sentences.map(sentence => sentence.split(" ")).flat();
console.log(sameResult); // ["hello", "world", "good", "morning"]
A useful trick: filtering and transforming in one step: return an empty array to skip an item.
let nums = [1, 2, 3, 4, 5]; let doubledEvens = nums.flatMap(n => (n % 2 === 0 ? [n * 2] : [])); console.log(doubledEvens); // [4, 8]
Things to Remember
- The default depth is only 1: deeper nesting stays nested unless you raise the depth.
- Use Infinity carefully: it is convenient, but make sure you really want every level flattened.
- It only flattens arrays: objects inside the array are left as they are.
let mixed = [1, [2, { a: [3] }]];
console.log(mixed.flat(Infinity)); // [1, 2, { a: [3] }]
- The copy is shallow: objects inside are shared with the original, not duplicated.
- It is a newer method: it is supported in modern browsers and Node.js, but not in very old ones.
Common Mistakes
- Expecting every level to be flattened: the default depth is 1, so deeper arrays stay nested.
- Forgetting to save the result: flat() does not change the original array.
- Using flat() on a plain array: it works, but nothing changes, so check that your data is actually nested.
- Using map() followed by flat() when flatMap() would do: flatMap() is shorter and clearer.
Conclusion
The flat() method turns a nested array into a simple list. Remember that the default depth is one level, that you can pass Infinity to flatten everything, and that flatMap() is the better choice when you need to transform items at the same time.
Frequently Asked Questions (FAQs)
1. Does flat() change the original array?
No. It returns a new array and leaves the original alone.
3. What is the difference between flat() and flatMap()?Use array.flat(Infinity).
flat() only flattens. flatMap() transforms each item first, and then flattens the result by one level.
0 Comments