Let's see an example first then I'll explain how the map() function works.
<body>
<p id="result"></p>
</body>
<script>
const arr = ['ab51', 'ad67', 'b-9', 'b81', 'cp11'];
const get_numbers_from_text = (val) => {
return val.replace(/\D+/g, '');
}
let a_new_array = arr.map(get_numbers_from_text);
let result = document.getElementById('result');
result.innerHTML = a_new_array;
</script>
</html>The map() function iterates or loops through an array using a callback function (or the mapping function). The call back function "get_numbers_from_text()" takes an argument, the array value. The function then returns only number after replacing the text (or string) value.
There's another method in JavaScript that you can use to perform a similar task, that is, filtering out only numbers from an array. See this example.
