I'm trying to create a function that maps an array to another newArray. If one of the array elements is a array
, I want to map all of its subelements to newArray.
Therefore:
var array = [1,2,3,4,5, [1,3,2,4,1]]
Should return
[1,2,3,4,5,1,3,2,4,1]
My code looks like this:
var array = [1,2,3,4,5, [1,3,2,4,1]]
var newArray = array.map(function(element){
if (typeof(element) === "object"){
element.forEach(function(subElement){
return(subElement);
})
} else {
return(element);
}
})
newArray;
But newArray returns:
[1, 2, 3, 4, 5, undefined]