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=0; i<Devices_List.length; i++)
{
if ( Devices_List[i].contentText == "")
// Deletes the item
{
Devices_List.DeleteItem[i]
}
}

Thanks in advance to everyone that helps me

  • 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]);
        }
    }
  • rraghvani's avatar
    rraghvani
    Champion Level 3

    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]);
        }
    }
    • NicolaFilosa_SE's avatar
      NicolaFilosa_SE
      Contributor

      Hi, thank you very much! To make it works I had to do little modifications but it works correctly

      var Devices_List = pageParameters01.FindAll("columnIndex", "3", 15, true).toArray()
      for (var i = 0; i < Devices_List.length; i++) {
      if (Devices_List[i].contentText == "") {
      Devices_List.splice(i, 1);
      break;
      }
      }

      for (var i = 0; i < Devices_List.length; i++) {
      Log.Message(Devices_List[i].contentText);
      }