最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i delete something out of an array? - Stack Overflow

programmeradmin0浏览0评论

My problem is that i have to delete something out of an array. I found out how to delete something out of a listbox. But the problem is that the listbox is filled by an array. So if I don't delete the value (I deleted out of the listbox) out of the array. The value keeps ing back when you add a new item. BTW: I am new to php and javascript.

My code is:

function removeItem(veldnaam){
var geselecteerd = document.getElementById("lst"+veldnaam).selectedIndex;
var nieuweArray;
alert(geselecteerd);    
alert(document.getElementById(veldnaam+'hidden').value);

For (var i = 0, i<= arr.lenght, i++) {
If (i= geselecteerd){
    nieuweArray = arr.splice(i,1);
    document.getElementById(veldnaam+'hidden').value = arr;
                }}

document.getElementById("lst"+veldnaam).remove(geselecteerd);
    }

My problem is that i have to delete something out of an array. I found out how to delete something out of a listbox. But the problem is that the listbox is filled by an array. So if I don't delete the value (I deleted out of the listbox) out of the array. The value keeps ing back when you add a new item. BTW: I am new to php and javascript.

My code is:

function removeItem(veldnaam){
var geselecteerd = document.getElementById("lst"+veldnaam).selectedIndex;
var nieuweArray;
alert(geselecteerd);    
alert(document.getElementById(veldnaam+'hidden').value);

For (var i = 0, i<= arr.lenght, i++) {
If (i= geselecteerd){
    nieuweArray = arr.splice(i,1);
    document.getElementById(veldnaam+'hidden').value = arr;
                }}

document.getElementById("lst"+veldnaam).remove(geselecteerd);
    }
Share Improve this question edited Jan 28, 2010 at 8:10 Stefan Gehrig 83.7k24 gold badges161 silver badges192 bronze badges asked Jan 28, 2010 at 8:04 stijnstijn 312 silver badges6 bronze badges 2
  • those dutch identifiers sound funny - I'd remend using english language only respecting the coding conventions... :D – Gnark Commented Jan 28, 2010 at 8:43
  • kk thanks for the advice Sophomore – stijn Commented Jan 28, 2010 at 8:45
Add a ment  | 

4 Answers 4

Reset to default 3

Use the delete operator. I'm assuming you are using objects as associative arrays.

var arr = {
  "hello": "world",
  "foo": "bar"
}
delete arr["foo"]; // Removes item with key "foo"

You can delete elements in an array using the delete mand. But it will just set the value to undefined.

var arr = ['h', 'e', 'l', 'l', 'o'];
delete arr[2];

arr => ['h', 'e', undefined, 'l', 'o'];

So it will not remove the item, and make a shorter array, the array will still have 5 elements (0 to 4), but the value has been deleted.

In the case of "associative" arrays, or objects: The property will get erased and it will no longer exist.

var obj = { 'first':'h', 'second':'e', 'third':'l'};
delete obj['first'];

obj  => { 'second':'e', 'third':'l'};

Add the following code somewhere


// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

and call it like this:


// Remove the second item from the array
array.remove(1);
// Remove the second-to-last item from the array
array.remove(-2);
// Remove the second and third items from the array
array.remove(1,2);
// Remove the last and second-to-last items from the array
array.remove(-2,-1);

Article containing the code above and explanation: http://ejohn/blog/javascript-array-remove/

var geselecteerd = document.getElementById("lst"+veldnaam).selectedIndex;
var nieuweArray;
var teller = 0;
var oudeArray=document.getElementById(veldnaam+'hidden').value;
var tmpArr="";


nieuweArray=oudeArray.split(":");

for (i = 0; i<nieuweArray.length; i++){
if (!(i==geselecteerd)){
tmpArr = tmpArr+nieuweArray[i]+":";}
teller++;
}
tmpArr = tmpArr + ":";
tmpArr = tmpArr.replace("::","");
document.getElementById(veldnaam+'hidden').value = tmpArr;          
document.getElementById("lst"+veldnaam).remove(geselecteerd);
}

This is my solution and it worked. Thanks for your help.

发布评论

评论列表(0)

  1. 暂无评论