function flatten(stack) {
if (!Array.isArray(stack)) {
throw new TypeError('not array');
}
 
let copy = stack;
const result = [];
 
while (copy.length) {
const currentEl = copy.shift();
 
if (Array.isArray(currentEl)) {
copy.unshift(...currentEl);
} else {
result.push(currentEl);
}
}
 
return result;
}
 
// Пример:
 
console.log(
flatten([1, 'any [complex] string', null, function() {}, [1, 2, [3, '4'], 0], [], { a: 1 }])
);
 
// должно вернуть [1, 'any [complex] string', null, function() {}, 1, 2, 3, '4', 0, { a: 1 }]
 
console.log(flatten([[0, 1], [2, [3, 4]]]));
 
// должно вернуть [0, 1, 2, 3, 4]
 
console.log(flatten([[1, 5]]));
 
// должно вернуть [1, 5]
 
Click Run to execute JavaScript.
Code runs in an isolated environment.