This question has already been answered:

The idea is that the code should cut out all the values ​​from the array, except digits, but it is generally unclear what is happening, maybe I somehow did not use the ForEach function;

 function filter_list(l) { l.forEach(onlyNumber) function onlyNumber(item, i, arr) { if(typeof item !== Number) { arr.splice(i, 1); } } return l; } console.log(filter_list(['a', 1, 3, 'b'])); 

Reported as a duplicate at Grundy. javascript 16 Sep '18 at 16:34 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    With the .splice() method, you change the array itself while running .forEach() . Use .filter() if you need to filter an array

     function filterList(arr) { return arr.filter(function (item) { return typeof item === 'number'; }); } console.log(filterList(['a', 1, 3, 'b'])); 
    1. The typeof operator returns the string