Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing an element from array

Status
Not open for further replies.

ob1kanobee

Programmer
Dec 27, 2002
47
0
0
US
Need help on removing an element from an array. Although this works, I'm hoping you have a better or more efficient way of accomplishing this:

function deleteRow(index)
{
for (var x = index; x < MyArray.length-1; x++)
{
MyArray[x] = ProfileArray[x+1];
}

MyArray.length = MyArray.length-1;
return;
}

MyArray[0] = ["1","2","3"];
MyArray[1] = ["4","5","6"];
MyArray[2] = ["7","8","9"];
MyArray[3] = ["A","B","C"];
MyArray[4] = ["D","E","F"];

deleteRow(2);

//This is the result I'm looking for after the method call...
MyArray[0] = ["1","2","3"];
MyArray[1] = ["4","5","6"];
MyArray[2] = ["A","B","C"];
MyArray[3] = ["D","E","F"];



 
not to sure but you just use
Code:
MyArray.splice(2,1);
//2 being the position and 1 being the amount after the position to remove
 
Yup:
Code:
<script language="javascript">
function debugarray(label, arr)
{	for( s=label+"\n", i=0; i< arr.length;i++ ) s+= arr[i].join(",") + "\n";
	alert(s);
}

[!]function deleteRow( arr, index )
{	arr.splice(index, 1);
}[/!]

var MyArray= Array();
MyArray[0] = ["1","2","3"];
MyArray[1] = ["4","5","6"];
MyArray[2] = ["7","8","9"];
MyArray[3] = ["A","B","C"];
MyArray[4] = ["D","E","F"];

debugarray("Before", MyArray);
deleteRow(MyArray, 2)
debugarray("After", MyArray);
</script>

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Thanks for the responses, I knew there had to be a better way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top