Search code examples
javascriptarraysparseint

How do I show only numbers from a Javascript array that has both numbers and letters use parseInt?


I'm trying to use the parseInt to show only numbers from the following array:

1,2,a,b

Here is my javascript code so far:

var filter_list = ["1,2,a,b"];

function myFunction() {
  parseInt(filter_list);
  return filter_list;
}

document.getElementById("display").innerHTML = filter_list;

Maybe my idea isn't even going to work. Would love some feedback.


Solution

  • You have array with one element that is string, so you could use join and split and then filter method.

    var data = ["1,2,a,b"];
    var numbers = data.join(",").split(",").filter(Number);
    console.log(numbers)