The function must project each element of the specified array to a sequence and flatten the resulting sequences into one array.
I have the function that must return the flattened array based on the given selector function (childrenSelector) but facing issues when applying slice() function.
When applying slice as a selector function, it says
TypeError: x.slice is not a function
function flattenArray(arr, childrenSelector) {
return arr.reduce((accumArr, currVal) => {
console.log(currVal);
return Array.isArray(currVal)
? accumArr.concat(currVal.map(childrenSelector))
: accumArr.concat(childrenSelector(currVal))
}, []
);
}
flattenArray([[11, 12, 13, 14, 15], [21, 22, ,23, 24, 25], [31, 32, 34, 35]], x => x.slice(0, 2))
Is there any solution for this issue?