
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to process JavaScript nested array and display the order of numbers according to the level upto which they are nested?
Suppose, we have a nested array of numbers like this −
const arr = [23, 6, [2,[6,2,1,2], 2], 5, 2];
We are required to write a program that should print the numbers (elements) of this array to the screen.
The printing order of the numbers should according to the level upto which they are nested.Therefore, the output for the above input should look like this −
23 6 2 6 2 1 2 2 5 2
Example
The code for this will be −
<!DOCTYPE html> <html> <head> <meta charset="utf−8"> <meta name="viewport" content="width=device−width"> <title>PATTERN</title> </head> <body> <script> const arr = [23, 6, [2, [6, 2, 1, 2], 2], 5, 2]; arr.reduce(function iter(level) { return function (node, item) { var pTag = document.createElement('p'); pTag.style.marginLeft = level + 'px'; node.appendChild(pTag); if (Array.isArray(item)) { item.reduce(iter(level || 50), pTag); } else { pTag.appendChild(document.createTextNode(item)); } return node; }; }(0), document.body); </script> <p></p> </body> </html>
And the output on the screen will be −
- Related Articles
- Sort nested array containing objects ascending and descending according to date in JavaScript
- How to count a depth level of nested JavaScript objects?
- Convert nested array to string - JavaScript
- JavaScript - summing numbers from strings nested in array
- Detecting the largest element in an array of Numbers (nested) in JavaScript
- Simplifying nested array JavaScript
- Product of numbers present in a nested array in JavaScript
- Aggregation: group date in nested documents (nested object) and display the count?
- Group objects inside the nested array JavaScript
- How to display only the keys from nested MongoDB documents?
- Grouping nested array in JavaScript
- How to sum all elements in a nested array? JavaScript
- Accessing and returning nested array value - JavaScript?
- How to convert nested array pairs to objects in an array in JavaScript ?
- Finding the maximum in a nested array - JavaScript

Advertisements