I want to remove the second element(" ") from the newArray
**Your code so far**
function steamrollArray(arr) {
let newArray = arr.toString()
newArray = newArray.split(/\,/g)
console.log(newArray)
newArray = newArray.map((val) =>{
if(val == "[object Object]"){
return {}
}
else if(val == ""){
return "how to remove this one "
}
else if(val.match(/[a-z]/g)){
return val
}
else{
return parseInt(val)
}
})
return newArray
}
console.log(steamrollArray([1, [], [3, [[4]]]]))
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Challenge: Steamroller
Link to the challenge:
garry2090:
val == "[object Object]"
I don’t think this will ever be true.
garry2090:
val.match(/[a-z]/g
And I’m not sure this is a useful case.
I think perhaps you are confused about how the data is structured. You are working with a nested array. Each element is either A) an element on its own or B) a sub-array of elements. You should only have two cases.
1 Like
U r right, I’m only making this funtion to somehow just complete the task(not universal) and that’s bad. Now I’m tryin’ again to make it useful for each case this time . Thanks btw
You can use the arrayObject.filter() method to remove elements from an array at specific index in JavaScript.
var rValue = 'three'
var arrayItems = ['one', 'two', 'three', 'four', 'five', 'six']
arrayItems = arrayItems.filter(item => item !== rValue)
console.log(arrayItems)
thanks buddy. This method is very useful
system
Closed
May 20, 2022, 9:05pm
6
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.