Flatten nested javascript array

Flatten nested javascript array

How do you flatten array in javascript

If you are given an array that contains literals, arrays and objects and you want to get all the values to one array.

Here is the snippet using recursive function to attain that.


function implode(arr) {
	var res = [];
	for (var i =0; i < arr.length ; i++) {
    	if (Array.isArray(arr[i])) {
        	res = res.concat(implode(arr[i]));
		} else if(typeof arr[i] == 'object' ) {
			var values = Object.values(arr[i]);
        	for (var j = 0 ; j < values.length ; j++) {
            	if (!Array.isArray(values[j]) && typeof values[i] != 'object') {
					res.push(values[j]);
                }else{
                	res = res.concat(implode(values[j]));
				}
			}
		} else {
			res.push(arr[i]);
        }
	}
	return res;
} 

how to get user input and process it in nodejs tutorial

how to display html files from node js example tutorial

a hello world tutorial using node js with http web server

node js mongo db and angular js tutorial for beginners from scratch

Redirect in Javascript

Select an element with multiple classes jQuery


Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/10/d419372832/htdocs/gullele/wp-includes/class-wp-comment-query.php on line 399

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*