Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Flattening Arrays with flat | Functional Patterns and Utilities
JavaScript Array Methods

bookFlattening Arrays with flat

Nested arrays are common in JavaScript, especially when representing data that naturally forms a hierarchy, such as lists of lists or grids. However, working with deeply nested arrays can make data manipulation more complicated. In many cases, you need to simplify these structures by reducing their nesting level—a process known as flattening. Flattening makes it easier to perform operations on all elements of an array without having to manage multiple levels of depth.

123
const numbers = [1, 2, [3, 4], [5, 6]]; const flattened = numbers.flat(); console.log(flattened); // Output: [1, 2, 3, 4, 5, 6]
copy

The flat method is a straightforward way to flatten arrays in JavaScript. By default, it reduces the nesting level by one, turning a two-dimensional array into a single-level array. However, arrays can have more than two levels of nesting. The flat method accepts an optional depth parameter, which determines how many levels of nesting should be flattened. If you pass a larger number as the depth, flat will continue flattening nested arrays up to that depth. For example, a depth of 2 will flatten arrays that are nested two levels deep. If you want to flatten all levels, you can pass Infinity as the depth.

1. Which of the following is the result of [1, [2, [3, [4]]]].flat(2)?

2. Fill in the blank to flatten the array const arr = [1, [2, [3, 4]]]; to a single-level array using the flat method.

question mark

Which of the following is the result of [1, [2, [3, [4]]]].flat(2)?

Select the correct answer

question-icon

Fill in the blank to flatten the array const arr = [1, [2, [3, 4]]]; to a single-level array using the flat method.

const arr = [1, [2, [3, 4]]]; const flatArr = arr.(2); console.log(flatArr); // Output: [1, 2, 3, 4]
[1, 2, 3, 4]

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

How do I use the flat method with a specific depth?

Can you show an example of flattening an array with more than two levels of nesting?

What happens if I use flat(Infinity) on a deeply nested array?

Awesome!

Completion rate improved to 8.33

bookFlattening Arrays with flat

Deslize para mostrar o menu

Nested arrays are common in JavaScript, especially when representing data that naturally forms a hierarchy, such as lists of lists or grids. However, working with deeply nested arrays can make data manipulation more complicated. In many cases, you need to simplify these structures by reducing their nesting level—a process known as flattening. Flattening makes it easier to perform operations on all elements of an array without having to manage multiple levels of depth.

123
const numbers = [1, 2, [3, 4], [5, 6]]; const flattened = numbers.flat(); console.log(flattened); // Output: [1, 2, 3, 4, 5, 6]
copy

The flat method is a straightforward way to flatten arrays in JavaScript. By default, it reduces the nesting level by one, turning a two-dimensional array into a single-level array. However, arrays can have more than two levels of nesting. The flat method accepts an optional depth parameter, which determines how many levels of nesting should be flattened. If you pass a larger number as the depth, flat will continue flattening nested arrays up to that depth. For example, a depth of 2 will flatten arrays that are nested two levels deep. If you want to flatten all levels, you can pass Infinity as the depth.

1. Which of the following is the result of [1, [2, [3, [4]]]].flat(2)?

2. Fill in the blank to flatten the array const arr = [1, [2, [3, 4]]]; to a single-level array using the flat method.

question mark

Which of the following is the result of [1, [2, [3, [4]]]].flat(2)?

Select the correct answer

question-icon

Fill in the blank to flatten the array const arr = [1, [2, [3, 4]]]; to a single-level array using the flat method.

const arr = [1, [2, [3, 4]]]; const flatArr = arr.(2); console.log(flatArr); // Output: [1, 2, 3, 4]
[1, 2, 3, 4]

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2
some-alt