John Cooper
John Cooper

Reputation: 7631

Adding elements to array problem

for(i=0;i<rowData.length;i++){
    if(rowData[i].title == 'Gender'){
        AddMinusGenderImage.remove(AddMinusGenderImage);
        rowData.splice(i,1);
        tableview.data = rowData;
        break;
    }
}

After i remove the data and a new data to the array, it's getting added to array[1] instead of array[0]. why??

Adding the Array Part

I add array to element on the click of the button like this. On each click of button, the row is getting added to array.

rowData.push(row);

row is an object, now it has a property title which i am checking and deleting the row. When i add another row, it's not taking the empty space instead creating a new array index.

Upvotes: 1

Views: 785

Answers (1)

Pointy
Pointy

Reputation: 413702

The ".splice()" function will remove an element from anywhere within your array, but ".push()" always adds elements to the end of the array.

Thus:

var r = ["hello", "out", "there"];

If we splice out the middle word ("out"):

r.splice(1, 1);

Then the array will be ["hello", "there"]. However, if we ".push()" something else:

r.push("up");

Then the array will be ["hello", "there", "up"].

Upvotes: 1

Related Questions