简体   繁体   中英

Length of the javascript array does not change after deleting the element from it

I am trying to copy one array values into another, but without breaking the links that is associated with this array other words i can not just assign the new array to this value, thats why i cannot use methods like slice() or concat(). Here is the code of the function that does the thing:

 self.updateBreadcrumbs = function (newBreadcrumbs) {
            var old_length = self.breadcrumbs.length;
            var new_length =newBreadcrumbs.length;
            var j = new_length > old_length ? new_length: old_length;

            for (var i = 0; i < j; i++) {
                if(old_length < i+1){
                    self.breadcrumbs.push(newBreadcrumbs[i]);
                    continue;
                }
                if(new_length < i+1){
                    delete self.breadcrumbs[i];
                    continue;
                }
                if (self.breadcrumbs[i].title !== newBreadcrumbs[i].title) {
                    self.breadcrumbs[i] = newBreadcrumbs[i];
                }

            }
        }

My problem is that length of the array does not change when i delete something from the array.

PS If you know any easier way to do this i am totally open for propositions.

Length of an Array can never change by deleting elements in it.

However It can be altered with splice eg.

var arr=[1,2,3,4,5]; //length 5
arr.splice(0,1); //length 4

Unlike what common belief suggests, the delete operator has nothing to do with directly freeing memory. delete is only effective on an object's properties. It has no effect on array length

The splice() method changes the content of an array by removing existing elements and/or adding new elements.

More about Splice

The length property of an Array object is a property that is maintained by it's methods, it is not an inherent characteristic of the object itself. So, if you take an action on it that is not one of Array 's own methods, it will not update the length property. In this case, it is the splice method that you would want to use ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice )

Interestingly, you can change the makeup of an array by directly altering its length . . . for example, if you have an array with 6 elements in it and you were to change its length property to 5 (ie, array.length = 5; ), it would no longer recognize the 6th element as being part of the array.

More on the length property: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length

An Array is (aside from its prototype methods) not much more than a simple object. The difference is in the length property which has a spcieal setter method, and the different way of adding a new property. If a property is added to the object, its name is tested for being a numerical value and whether this value is >= the current array length. if so, the internal length is updated. If the length is set, it is checked, whether the new value is smaller than the old one, and if so, all own properties with a property name that is a numerical value and >= the new length will be deleted.

If you delete an indexed element from an array, it will just be deleted (and accessing this index later will return undefined, as it would do with any non-existent property). It won't change the array length, nor will increasing the array length create any properties for the added index positions (sparse array). Using [] or new Aray[10000] will both create Arrays with exactly the same memory footprint. Just the length property has a different initial value.

This is completely different from Arrays in other languages, where an array of size n will have n storage spaces reserved for the elements (being pointers to objects or being native value types). So in other languages, creating an array with a given size and filling it is faster than creating an empty array and adding values (in which case each time a larger consecutive space must be allocated and the array must be copied to this larger memory psoition before adding the element). OTOH, accessing the elements of an JS array is somewhat slower (as they are just named properties which have to be located in the property list first) than in other languages where an element is at its given, fixed position.

This answer will not help if you use array, but.

If you still need to delete item by key and you can use object:

$o = {'blue' : '', 'apple' : '', 'red' : ''};

console.log(Object.keys($o).length); // will output 3 
delete $o['apple'];
console.log(Object.keys($o).length); // will output 2

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