theJava
theJava

Reputation: 15044

removing an element from array

rowData = [];

alert(rowData[0]);

gives me [Ti.UI.TableViewRow]

Now how can i remove this element... i have been using rowData.splice(), but i have no idea on what to pass to remove it.

Thanks

Upvotes: 0

Views: 225

Answers (3)

KooiInc
KooiInc

Reputation: 122986

In the code you present rowData should be empty, so rowData[0] should be undefined. I suppose something is pushed to rowData in between? Anyway, there are several ways to remove elements from arrays:

  • You can remove all elements at once from an array using rowData.length = 0.
  • If you want to remove 1 element, use the Array.splice method. E.g. removing the first element: rowData.splice(0,1) (means remove 1 element of rowData starting from element 0 (the first element).
  • If it's only the first element you want to remove you could also use the shift method: rowData.shift().
  • The last method you can use is slice: rowData = rowData.slice(1) (means: give me all elements from rowData starting at the first element and assign the result to rowData), or rowData.slice(1,4) (means: give me all elements from rowData starting at the first element, ending at the fourth element, and assign the result to rowData).

Upvotes: 2

alex
alex

Reputation: 490597

If you want to remove the element(s) entirely, splice() will return a new array with the member(s) removed.

You can also use the delete operator, but this won't affect the Array size and the member will be undefined. This is will also make it non enumerable.

Upvotes: 1

otakustay
otakustay

Reputation: 12425

try rowData.splice(0, 1); the first argument indicates the index of item to be removed, the second indicates how many items should be removed

Upvotes: 3

Related Questions