Forum Discussion

NicolaFilosa_SE's avatar
NicolaFilosa_SE
Contributor
2 years ago

Delete particular element of an array

Hi to everyone, I have a problem handling an array. I have an array that contains a null element (""), and I want to remove this element from the array. I tried it but it does not work: for (var i=...
  • rraghvani's avatar
    2 years ago

    See Array.prototype.splice() for example,

    function MyArray()
    {
        var myArray = ["One", "Two", "Three", "", "Five"];
        for (var i = 0; i < myArray.length; i++) {
            if (myArray[i] === "") {
                myArray.splice(i, 1);
                break;
            }
        }  
    
        for (var i = 0; i < myArray.length; i++) {
            Log.Message(myArray[i]);
        }
    }