views:

56

answers:

3

Hello,

i have the current .each in my javascript

var divs = array();
$('.div-item').each(function(){
   var id = $(this).attr('id');
   divs.push(id);
});

How would I delete an id from the array programmatically?

A: 
var list = [4,5,6];
list.splice(1, 1); // Remove one element, returns the removed ones.

list now equals [4,6]

mkoistinen
is there a way for instance i want to remove 5 just do like .splice(5)? since i wouldn't know exactly the index of the value i want to remove?
William Smith
See sje397's wrapper in his answer.
mkoistinen
+2  A: 
function deleteId(divArray, id) {
  var idx = $.inArray(id, divArray);
  if(idx != -1)
    divArray.splice(idx, 1);
}

EDITED: to use $.inArray: some versions of IE don't support the indexOf method on arrays.

sje397
So can it be deleteId(divs, "product_1"); ?
William Smith
Yep............
sje397
Thanks works like a charm. Do you have a way i can chat with you have an offer.
William Smith
A: 

You can use jQuerys .map() to create an array like this:

var divs = $('.div-item').map(function(i, e){
    return this.id;
}).get();

To delete one entry, use Javascripts .splice():

divs.splice(3,1) // removes forth element

Syntax of .splice() is startIndex, number of elements, new elements (optional)

jAndy