I have the below function, but it is not returning the desired result. How can I change this so it returns the desired output?
function convertArr () {
const separateNestedArrays = function (arr) {
return arr.flatMap((element) => {
if (Array.isArray(element)) {
return element;
} else {
return [element];
}
});
};
const twoDimensionalArray = [
[1, 2, 3, 4],
[[5, 6], [9, 10]],
[5, 6, 7]
];
const modifiedArray = separateNestedArrays(twoDimensionalArray);
console.log(modifiedArray);
// Desired Output: [[1, 2, 3, 4], [5, 6], [9, 10], [5, 6, 7]]
// Current Output: [ 1, 2, 3, 4, [ 5, 6 ], [ 9, 10 ], 5, 6, 7 ]
};
Via Active questions tagged javascript - Stack Overflow https://ift.tt/IR32a6p
Comments
Post a Comment