-2

Possible Duplicate:
JavaScript Array Delete Elements

How is this done?

If I have the following array defined:

var myArr = [];
myArr[id1] = {prop1: prop1Value, prop2:prop2Value};
myArr[id2] = {prop1: prop1Value, prop2:prop2Value};
//etc

I wish to delete myArr[id1]

2
  • Dear Patricia, it's not a duplicate. No answer in the question above answers my question. If you think otherwise, it would be great if you posted some code that does. Regards.
    – sarsnake
    Nov 17, 2011 at 19:06
  • Please note that this is an associative array so it lacks splice(), hence the "duplicate" question doesn't actually answer this one. Please actually READ the question before clicking to close.
    – sarsnake
    Nov 17, 2011 at 19:27

1 Answer 1

4

Perhaps you mean that the numerical indices after the deleted element aren't updated... in which case you'll need to use splice:

myArr.splice(id1, 1);
2
  • no splice() on associative arrays in JS
    – sarsnake
    Nov 17, 2011 at 19:28
  • @sarsnake Because associative arrays are just objects, and the only way to delete a property of an object is to use delete: delete myArr[id1];. The reason myArr[id1] still returns undefined is because that's what happens when you check a property that doesn't exist: myArr.fippetyFloppityFloo also returns undefined... Nov 17, 2011 at 20:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.