How to delete an array element in javascript without using any of built-in Array methods? -
i want write own implementation of array element deletion method. why? - because interested know algorithm behind array element deletion , take stab @ that.
i tried delete operator sets element undefined. don't want use of existing built-in methods of arrays such splice, shift, pop, etc.
is possible achieve this? not looking whole script idea , direction on how proceed help. may suggest me topics explore answer.
thanks.
p.s-1: have seen other posts related array element deletion use splice, pop, shift methods.
p.s-2: if going downvote question atleast have courtesy explain why downvoting. otherwise assume life extremely miserable , need downvote random questions posted on forum cure misery.
you can create own deletion method:
array.prototype.flush = function() {console.log(this.length = 0);} usage:
var test = [1, 2, 3]; alert(test); test.flush(); alert(test); update 1 deletion method: if understand correct: specify position, delete 1 element. if not better use array.prototype.splice(), anyhow, if asked:
code:
array.prototype.delete = function(pos){ if (pos < this.lenght) { this.splice(pos, 1); return } throw "index " + pos + " outside of array length"; } usage:
var test = [1, 2, 3]; alert(test); test.delete (1); alert(test); update 2 javascript array delete without prototype methods usage:
array.prototype.delete = function (pos) { if (!this.length) throw 'array empty'; if (pos < this.length) { (var = pos; < this.length - 1; i++) { if (i < pos) continue; this[i] = this[i + 1]; } this.length = this.length - 1; return this; } throw 'index ' + pos + ' outside of array length'; } var foo = [1, 2, 3]; foo.delete(0); alert(foo); foo.delete(0); alert(foo); foo.delete(0); alert(foo); foo.delete(0); alert(foo);
Comments
Post a Comment