- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert nested array to string - JavaScript
We are required to write a JavaScript function that takes in a nested array of literals and converts it to a string by concatenating all the values present in it to the string
const arr = [ 'hello', [ 'world', 'how', [ 'are', 'you', [ 'without', 'me' ] ] ] ];
Example
Let’s say the following is our nested array −
const arr = [ 'hello', [ 'world', 'how', [ 'are', 'you', [ 'without', 'me' ] ] ] ]; const arrayToString = (arr) => { let str = ''; for(let i = 0; i < arr.length; i++){ if(Array.isArray(arr[i])){ str += arrayToString(arr[i]); }else{ str += arr[i]; }; }; return str; }; console.log(arrayToString(arr));
Output
Following is the output in the console −
helloworldhowareyouwithoutme
- Related Articles
- How to convert nested array pairs to objects in an array in JavaScript ?
- Convert integer array to string array in JavaScript?
- How to convert an array into JavaScript string?
- JavaScript - convert array with null value to string
- Simplifying nested array JavaScript
- Convert string with separator to array of objects in JavaScript
- MongoDB query to get array of nested string?
- How to convert string type value to array type in JavaScript?
- Grouping nested array in JavaScript
- How to convert a 2D array to a CSV string in JavaScript?
- Query array of nested string with MongoDB?
- Recursion - Sum Nested Array in JavaScript
- Join in nested array in JavaScript
- Transform nested array into normal array with JavaScript?
- How do you convert a string to a character array in JavaScript?

Advertisements