Log in

View Full Version : Delete operator javascript



jscheuer1
04-07-2016, 08:51 PM
I look this thing up on average maybe once a month (never seem to be sure of the syntax), but never really use it. It's fun in the console, but never seems to do much in real life. Anyone really use this thing in scripts and/or rely upon it much?

molendijk
04-07-2016, 10:38 PM
I've never used it, but I can imagine situations where using the delete operator could be useful.
Suppose you have an array of elements all of which your want to show on all the pages of your site except on a page called 'delete.html'. On 'delete.html', you don't want to show element[2] (that's me alright). Code could be:

<script>
var names = ["Beverleyh","John","Arie","Ludwig Van"];
</script>

<a href="javascript: void()" onclick="if(document.URL.substring(document.URL.lastIndexOf('/')+1, document.URL.length)=='delete.html'){delete names[2]; document.getElementById('names').innerHTML=names}; return false">show celebrities</a>

<div id="names"></div>
I know that we will have the same result if we have names[2]=undefined instead of delete names[2], but this is only a simple example. If we replace the array of names above with an arry of images in a gallery show, the delete operator will make sure that a certain image will not be displayed on 'delete.html', whereas the use of undefined will replace that image with a 'blank' (I guess).

jscheuer1
04-07-2016, 11:45 PM
I guess I'm just a pack rat. I never want to throw anything away. ;) You have a point, deleting it is perhaps better than having it undefined, except I'm uncertain of the ramifications upon indexing in an array. With an object though, it's the way to go, except it could always be false, whatever, and you could test for that rather than outright deletion. But once an object gets big, it might be better to delete. I like objects because one can do things like:


if((spcld = stg.dates[dn + '']) || (spcld = stg.dates[(mn + 1) + ',' + dn]) || (spcld = stg.dates[yr + ',' + (mn + 1) + ',' + dn])){
$(td).addClass('jqspcldate').attr('title', spcld.title || '').click(spcld.click || function(){});
}


It's a flexible lookup for (in this case) entries that might have various keys relating to year month and day, or month and day, or day. I often wonder if the lookup takes longer the more entries there are. You do no enumeration or other for i or for p in type of running over the object. It's a lot like if you do if (array[6] && array[6] !== undefined) {whatever;}, but more flexible. Does it matter how large the array or object is in either case? If so, deleting things from the object would be a good idea when they're no longer needed.

jscheuer1
04-08-2016, 12:42 AM
I just discovered something pretty cool about delete. You can delete a property that doesn't exist. As long as the object exists, apparently that's OK. Like you can:


delete window.nowaythisisawindowpropertyunlesssomeonesetit

And even if there is no window.nowaythisisawindowpropertyunlesssomeonesetit, you get no error. That seems handy.

molendijk
04-08-2016, 10:07 AM
You have a point, deleting it is perhaps better than having it undefined, except I'm uncertain of the ramifications upon indexing in an array.
I don't think there will be indexing problems. If we replace the onclick in my code with this:
<a href="javascript: void()" onclick="if(document.URL.substring(document.URL.lastIndexOf('/')+1, document.URL.length)=='delete.html'){delete names[2]; document.getElementById('names').innerHTML=names}; alert(names[3]); return false">show celebrities</a>

we get Ludwig Van in the alert, which is correct.

EDIT
I found this at http://stackoverflow.com/questions/5767325/remove-a-particular-element-from-an-array-in-javascript:
To remove an element of an array at an index i: array.splice(i, 1).
If you just want to make the element at index i no longer exist, but you don't want the indexes of the other elements to change: delete array[i].

jscheuer1
04-08-2016, 03:33 PM
Yeah, I hate undefined though when it's still at an index because it can be converted to a string when iterating over the array, and since the indexes in an array (unlike in my test array below) usually mean nothing, I'd rather splice an array. From the console:


var a = [0, 1, 2, 3]
undefined
a
[0, 1, 2, 3]
delete a[2]
true
console.log('A cat ate my homework and ' + a[2]);
A cat ate my homework and undefined
undefined
a[1] || console.log('A cat ate my homework and ' + a[2]);
1
a[2] || console.log('A cat ate my homework and ' + a[2]);
A cat ate my homework and undefined

With proper testing it's fine. But a major speed advantage in an array is being able to assume everything is defined when iterating over it. Objects are slower because you either have to test each entry as to whether or not it's an 'ownProperty' of the object or just do single item lookups testing each to see if it exists before using it, the latter being the more proper use of an object in my opinion.