简体   繁体   中英

How can i delete something out of an array?

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 coming 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);
    }

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 command. 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.org/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM