After searching through numerous similar questions, I have been unable to find a solution to this particular challenge. I am in need of a function that can remove all non-number items from an array while also modifying the existing array rather than creating a new one. My attempt so far looks like this:
let array = [1, 'a', 'b', 2];
array = array.filter(item => typeof item !== "string");
console.log(array);
//--> [1, 2]
While this code gives me the desired output, it falls short on some additional requirements. According to MDN, the result creates a new array with the filtered values, which I did not fully consider.
If anyone has more experience in solving this kind of problem, I would greatly appreciate your help. I've spent hours attempting various solutions involving array.splice
and for
loops, but have not come close to success. Thank you in advance for any assistance, and apologies for the lengthy post.