1

I am going through the process of creating a bunch of array's , adding elements with push() and getting back a few arrays with a bunch of elements based on the process used to get the elements on the ui.

So i end up with something like this.

[Object, Object, Object]
0: Object
  parcels: Array[1]
  ref: "IDE25.8.2013.0637548291"
  region: "remote"
  service: "Early Delivery"
  totalPrice: 210
  weight: "1"

1: Object
  parcels: Array[1]
  ref: "IDE25.8.2013.1507892643"
  region: "remote"
  service: "Saturday"
  totalPrice: 135.67
  weight: "1"

2: Object
  parcels: Array[1]
  ref: "IDE25.8.2013.1507432643"
  region: "remote"
  service: "Saturday"
  totalPrice: 115.67
  weight: "1"

From what i have learnt in order to "remove" one of the objects you need to add a delete

So i use

delete consignmentsArr[1];

Now i would assume that the delete would delete the array , but it does not. One is left with what is called a sparse array. So the array does not exist in that is cannot be used, in that it's elements and the objects are not defined but it still exists in the index of the group of arrays as an

[Object, undefined × 1, Object]

This however destroys the logic that has been created with the ui elements that display the elements in the other arrays.

I am not really trying to find out how to fix this problem in my application. What i want to know its why is this the default behavior in javascript? Why would a delete not just be a delete as the name implies, and then reorder the index based on the objects that still exist? Why would leaving it as a sparse array be of any benefit in the development process? Also ... why not rename to

sparse consignmentsArr[1];

4 Answers4

1

I think you mean to use the splice method.

consignmentsArr.splice(1, 1);

Delete is for removing properties from objects.

Andy
  • 39,764
  • 8
  • 53
  • 80
  • Well I did actually start with the splice method. But from what i can tell the splice removes elements of an array not the array itself. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice. When i used splice it would delete more then 1 array even when (1,1). But that is not really what i am asking here. –  Sep 25 '13 at 12:38
  • If you want to delete the array it's self you have to make sure it is a property of an object, so `var a = []; delete a;` does not work, you would have to do `this.a = []; delete this.a'` (`this` can be some other object, even window). – Ally Sep 25 '13 at 12:43
1

Here is a possible explanation of the Javascript delete operator (as used on an Array):

For Javascript arrays, the delete operation is O(1), because it just mutates the value of a random access array to undefined.

The operation which would reorganize the indices of the array like so (in pseudo javascript):

a = [1,2,3] -> delete a[1] -> a is now [1,3]

would be O(N) because in the worst case, you would delete the first element and the rest of the array would have to be shifted.

See http://en.wikipedia.org/wiki/Array_data_structure and read the part under the header Efficiency.

Addition: Another explanation of the behaviour of the delete operator on arrays is so that the delete operator on arrays does the same thing as the delete operator on other objects.

If I have a given object: a = {'hi': 'hello', 'bye': 'good bye'} and I delete a['hi'] then a['hi'] becomes undefined.

If I have an array arry = [1,2,3], then delete arry[1] then arry[1] also becomes undefined.

DJG
  • 5,903
  • 3
  • 27
  • 50
1

I think, delete operator doesn't means remove an item in array, it just means unlink a object from the index you given.

Think about the consignmentsArr is a container, there are three boxes in it.
And in each box has an apple.
delete consignmentsArr[1] means take the apple away from the second box.
Do you think the third box at before, now is the second box? ^_^

anjianshi
  • 63
  • 1
  • 7
  • How would this not be helpful in development purposes? Its cute that you are using boxes to describe this scenario however the representation of these boxes are linked to ui elements which are defined on actions based on the users input. I want to delete the box not the apples. There should be three boxes each box has apples. –  Sep 25 '13 at 12:55
  • This makes a good point, delete is unlinking an object. Which means the garbage collector will know it has no more existing references and can now safely delete it. – Ally Sep 25 '13 at 13:13
0

The delete operator removes a property from an object.

You can use either of the following

delete object.property
delete object['property']

delete is NOT an operator on array. that is why reordering the index is not applicable in the context of delete.

You can still use splice to totally remove an item from array and reorder the index.

arr.splice(1, 1);
Mohayemin
  • 3,761
  • 4
  • 23
  • 51
  • This is where i am confused though because splice can also be used for elements of an object in an array –  Sep 25 '13 at 12:43
  • @gerdi: "splice can also be used for elements of an object in an array" - i did not understand it. – Mohayemin Sep 25 '13 at 12:48
  • well according to this documentation by mozilla https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice splice targets elements of an array not an array object itself –  Sep 25 '13 at 12:49
  • @gerdi: Splice removes the items and reorders the index of the array itself. Do you need something else? – Mohayemin Sep 25 '13 at 12:52
  • Well I donts want to remove items .. i want to remove an array –  Sep 25 '13 at 12:56