Рекурсия есть:
const getPrimitives = data =>
data instanceof Object
? Object.values(data).flatMap(getPrimitives)
: [ data ];
Рекурсии нет:
function getPrimitives(data) {
const result = [];
const stack = [];
for (let i = 0, arr = [ data ]; i < arr.length || stack.length; i++) {
if (i === arr.length) {
[ i, arr ] = stack.pop();
} else if (arr[i] instanceof Object) {
stack.push([ i, arr ]);
[ i, arr ] = [ -1, Object.values(arr[i]) ];
} else {
result.push(arr[i]);
}
}
return result;
}
// или
function getPrimitives(data) {
const result = [];
for (const stack = [ data ]; stack.length;) {
const n = stack.pop();
if (n instanceof Object) {
stack.push(...Object.values(n).reverse());
} else {
result.push(n);
}
}
return result;
}
// или
function getPrimitives(data) {
const result = [].concat(data);
for (let i = 0; i < result.length; i++) {
const n = result[i];
if (n instanceof Object) {
result.splice(i--, 1, ...Object.values(n));
}
}
return result;
}