mardi 21 avril 2020

Error deleting a single value that is multiple times in the same array with splice() [duplicate]

I am trying to delete a specific value from an array that contains this same value several times, but splice() don't remove all the values from the array.

This is the code:

let arr = ['402', '556', '402', '556', '556', '556', '556',];

console.log(`=> BEFORE DATA: ${arr}`);

arr.forEach(function (val, index) {
        console.log(`ARRAY DATA FIRST #${index + 1}: ${arr}`);
        if (val === '556') {
                arr.splice(index, 1);
        }
        console.log(`ARRAY DATA LAST #${index + 1}: ${arr}`);
});

console.log(`=> AFTER DATA: ${arr}`);



Aucun commentaire:

Enregistrer un commentaire