Question

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];
Was it helpful?

Solution

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.

OTHER TIPS

I think you mean to use the splice method.

consignmentsArr.splice(1, 1);

Delete is for removing properties from objects.

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? ^_^

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top