Alternative to array.splice in JavaScript

nns picture nns · Nov 21, 2016 · Viewed 8.3k times · Source

I am currently working on a project where I store numeric values in a JS array. After some changes it should be removed again. I currently use the array.splice method like this:

function removeA(arr, element) {
    var index = arr.indexOf(element);
    if (index >= 0) {
        arr.splice(index, 1 );
    }
    return arr;
} 

But this seems to give me issues on Safari. This piece of code works in every browser, like Chrome, Firefox, Opera. But not on Safari. It even works in the Technical Preview of Safari.

Does anyone have an alternative?

Thanks in advance :)

Answer

kevin ternet picture kevin ternet · Nov 21, 2016

You can use the built-in filter()

var array = [1,2,3,7,4,5,6,7,12,54,7,691];
var array = array.filter(x => x !== 7);
console.log(array);