Bryntum Quiz 2

by Alexander Novikov

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

/*
2. Given an arbitrary array which potentially contains other flat arrays:
 [ [1, 2, “a”], 3, “foo”, [ “bar”, {} ] ]
How do you transform this into a completely flat array?
[ 1, 2, “a”, 3, “foo”, “bar”, {} ]  
Try to find the shortest code in pure JavaScript to solve this task.
*/
var a = [ [1, 2, 'a'], 3, 'foo', [ 'bar', {} ] ];

var i = a.length;

while (i--) {
    
    if (Object.prototype.toString.call(a[i]) === '[object Array]') {
        
        Array.prototype.splice.apply(a, [i, 1].concat(a[i]));
        
    }
    
}

console.log(a);