Since the purpose of the question has become more clear now, an edit:
var a = ["1", 'car', 2];
a = a.map(function(element) {
//check if element is a number and not NaN or empty string
if (!isNaN(element) && element != "") {
return parseInt(element, 10);
}
else
{
return element;
}
});
document.body.textContent = a;
This will traverse your array. Testing if the array element is a number, if so transform the number if possible. I’m using Array.prototype.map
for this. Please note that this only filters integers. If you want to filter floats use parseFloat
. That will parse integers and floats.
7
solved Calling .parseInt() on something that can’t be parsed – JavaScript