The following code will execute properly in the sandbox:
const concatR = function(arr, ...args) {
let arr2 = (unnest = elem => !Array.isArray(elem) ? elem : elem.flatMap(unnest))(args);
return arr.concat(arr2);
};
let x2 = concatR([1,2,3], 'hello', 'new', [1,2,3], [1,2,[3,4]]);
console.log(x2);
However, on my VS Code with Node.js v14.18.0, it gives an error:
ReferenceError: unnest is not defined
Is there a certain feature that (I think?) allows a variable definition to pass a named arrow function, or what feature exists in the code sandbox that isn't on my local machine? Locally, I have to separate the statements:
const concatR = function(arr, ...args) {
const unnest = elem => !Array.isArray(elem) ? elem : elem.flatMap(unnest);
let arr2 = unnest(args);
return arr.concat(arr2);
};
let x2 = concatR([1,2,3], 'hello', 'new', [1,2,3], [1,2,[3,4]]);
console.log(x2);