Implement Custom Array Flat method in JavaScript Last Updated : 01 Apr, 2024 Comments Improve Suggest changes Like Article Like Report The flat() method in JavaScript is used to flatten nested arrays. By using that function we can directly convert any type of array into 1D array. These are the following approaches to implementing a custom array flat method in JavaScript: Table of Content Using Recursion approachUsing Iterative approachUsing Recursion approachThis approach uses a recursive function to flatten the nested arrays. Here function checks each element of the array and if the element is an array then it recursively calls itself to flatten that array. Example: The below code uses Recursion to flatten the array. JavaScript function customFlat(arr) { return arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? customFlat(val) : val), []); } const nestedArray = [1, [2, 3], [4, [5, 6]]]; console.log("Flattened array is: ") console.log(customFlat(nestedArray)); OutputFlattened array is: [ 1, 2, 3, 4, 5, 6 ] Using Iterative approachThis approach uses a while loop to iterate over the array. It keeps track of the current index in the array and checks if the element at that index is an array. If it is an array, it flattens that array and inserts its elements into the original array at the current index. Example: The below code uses an Iterative approach to flatten the array. JavaScript function customFlat(arr) { let flattened = [...arr]; let i = 0; while (i < flattened.length) { if (Array.isArray(flattened[i])) { flattened.splice(i, 1, ...flattened[i]); } else { i++; } } return flattened; } // Example usage const nestedArray = [1, [4, [5, 6]], [[8,9], 10]]; console.log(customFlat(nestedArray)); Output[ 1, 4, 5, 6, 8, 9, 10 ] Comment More infoAdvertise with us Next Article Implement Custom Array Flat method in JavaScript G ghuleyogesh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Convert an Array into a String We have been given an array as an input that consists of various elements and our task is to convert this array of elements into a string by using JavaScript. Below we have added the examples for better understanding:Example: Input: arr= [1,2,3,4,5,6]Output: 1,2,3,4,5,6Table of ContentUsing arr.join 4 min read Convert 2D Array to Object using Map or Reduce in JavaScript Converting a 2D array to an object is a common task in JavaScript development. This process involves transforming an array where each element represents a key-value pair into an object where keys are derived from one dimension of the array and values from another. Problem Description:Given a 2D arra 2 min read JavaScript Program to Transform Nested Array into Normal Array JavaScript allows us to convert nested arrays into a single, flat array using JavaScript. By simplifying nested structures, you can enhance the manageability and readability of your code. Example: Input: [[1, 2], [3, [4, 5]]] Output: [1, 2, 3, 4, 5]Below are the approaches to transform nested arrays 3 min read Sparse Table Using JavaScript Array In this article, we are going to learn about Sparse Table using JavaScript array. Sparse Table is a data structure in JavaScript used for efficient range queries (e.g., minimum or maximum) on an array. It precomputes and stores values to answer queries quickly, reducing time complexity. Example: Inp 3 min read JavaScript Program to Create an Array with a Specific Length and Pre-filled Values In JavaScript, we can create an array with a specific length and pre-filled values using various approaches. This can be useful when we need to initialize an array with default or placeholder values before populating it with actual data.Table of ContentMethod 1: Using the Array() Constructor and fil 3 min read JavaScript Program to Split Map Keys and Values into Separate Arrays In this article, we are going to learn about splitting map keys and values into separate arrays by using JavaScript. Splitting map keys and values into separate arrays refers to the process of taking a collection of key-value pairs stored in a map data structure and separating the keys and values in 5 min read JavaScript Program to Create an Array of Unique Values From Multiple Arrays Using Set Object We have given multiple arrays consisting of numerical values, and our task is to create an output array that consists of unique values from the multiple input arrays. We will use the Set object in JavaScript language. Example: Input: inputarray1: [1,2,3,4,5] inputarray2: [4,5,6,7,8] Output: outputAr 5 min read JavaScript Array flatMap() Method The flatMap() method transforms each element of an array using a mapping function and flattens the result into a new array. It applies the function to every element, avoiding empty ones, and preserves the original array. Syntax:let A = array.flatMap(function callback(current_value, index, Array)) {/ 3 min read JavaScript Array flat() Method The Javascript arr.flat() method was introduced in ES2019. The flat() method in JavaScript creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. If no depth is provided, it defaults to 1.Syntax:arr.flat([depth])Parameters:This method accepts a si 4 min read JavaScript Array Iteration Methods JavaScript Array iteration methods perform some operation on each element of an array. Array iteration means accessing each element of an array. There are some examples of Array iteration methods are given below: Using Array forEach() MethodUsing Array some() MethodUsing Array map() MethodMethod 1: 3 min read Like