MagnoliaKobus
12-26-2003, 10:24 PM
I'd like to know how can I remove the array elements in javascript .
|
Click to See Complete Forum and Search --> : Removing elements from array MagnoliaKobus 12-26-2003, 10:24 PM I'd like to know how can I remove the array elements in javascript . fredmv 12-26-2003, 11:59 PM Welcome to the forums. Use the delete operator:delete foo[n];Where n is the element in the array you want to remove. MagnoliaKobus 12-27-2003, 02:56 AM I got it , Thanks for your help . fredmv 12-27-2003, 03:14 AM No problem. ;) JayDie 03-25-2004, 05:16 AM Yes, I understand that you can delete an entry by executing: delete array[x] But when I got this array: var array= new Array() array[array.length]='one' array[array.length]='two' array[array.length]='three' array[array.length]='four' and I say delete array[1] the enry with value 'two' will be deleted. Then, the content of the array isn't 'one, three, four' but 'one, undefined, three, four'. I want that entry 1 is three and not undefined. Is this possible with a excisting function or do I have to write my own? JayDie JayDie 03-25-2004, 06:24 AM See the post before: I quess I only can solve this problem if I get the entries of the array before the 'undefined' and get the entries of the array after the 'undefined' and create a new array which consists of the two new array's before...? Using splice and/or slice...? JayDie JayDie 03-25-2004, 07:38 AM ...using slice and concat methods. NO use of delete. <HTML> <HEAD> <SCRIPT language="JavaScript"> var tabel = new Array() tabel[tabel.length]='een' tabel[tabel.length]='twee' tabel[tabel.length]='drie' tabel[tabel.length]='vier' function showArray() {alert(tabel +', length = ' +tabel.length) } function remFromArray(pos) {if(pos<tabel.length) { if(pos==0) // first entry tabel = tabel.slice(1) else if(pos==(tabel.length - 1)) //last entry tabel = tabel.slice(0, -1) else {aBefore = tabel.slice(0,pos) aAfter = tabel.slice(parseInt(pos) + 1) tabel=new Array() tabel = tabel.concat(aBefore, aAfter) } alert(tabel) } else alert('fout') } </SCRIPT> </HEAD> <BODY> <FORM> <INPUT type=button onClick="showArray()" value="Show Array"><BR> <INPUT type=text id=pos value=0>(zero based)<BR> <INPUT type=button onClick="remFromArray(document.getElementById('pos').value)" value="Remove 'een' from Array"><BR> </FORM> </BODY> </HTML> Greetings... JayDie steelersfan88 03-25-2004, 02:12 PM A newer way to perform this is simply use the splice method of an array, yet this is new to browsers, so I am not sure how compatible it is:<script type="text/javascript"> var nums = new Array // create array of numbers nums[nums.length] = "zero" nums[nums.length] = "one" nums[nums.length] = "two" nums[nums.length] = "three" nums[nums.length] = "four" var temp = nums.splice(1,2) // remove nums 1 to 2 (remove one and two form array) temp = "" // clear temp alert(nums) // alert content of nums array </script> webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |