0

I want to write my own implementation of array element deletion method. Why? - Because I am interested to know the algorithm behind the array element deletion and would like to take a stab at that.

I tried delete operator but it just sets the element to undefined. I don't want to use any of the existing built-in methods of Arrays such as splice, shift, pop, etc.

Is it even possible to achieve this? I am not looking for whole script but an idea and a direction on how to proceed would help. May be suggest me the topics I could explore to get an answer.

I have seen other posts related to array element deletion but they all use splice, pop, shift methods.

4
  • 1
    Why do you want to re-invent the wheel? Split, pop, shift, .. works just fine? What are you really trying to achieve? Commented May 11, 2015 at 12:11
  • "my own implementation of array element delete method": Array.prototype doesn't have a delete method. That method only applies to objects. Commented May 11, 2015 at 12:12
  • 1
    I wanted to learn the internals and was wondering if you anyone have tried this. I am not trying to re-invent the wheel. This is just curiosity on how deletion work internally. No, this is not a homework but more of curiosity. Commented May 11, 2015 at 12:31
  • @Andy: I understand that Array doesn't have delete method. It's just a sentence to explain what I wanted to do. Commented May 11, 2015 at 12:36

2 Answers 2

1

You can create your 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 I understand correct: specify position, delete one element. If not better use is Array.prototype.splice(), anyhow, if you asked:

Code:

Array.prototype.delete = function(pos){ 
 if (pos < this.lenght) {
  this.splice(pos, 1);  
  return this
 } 
 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 is empty';
  if (pos < this.length) {
    for (var i = pos; i < 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);
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah something like that. I was trying the same logic earlier by shifting the elements. It works but if the array is really large it will result in lot of assignment operations. So I was wondering if there is a effective way. I was also googling to find the implementation details of splice(), slice(), etc of Array.prototype but still searching. Thanks for taking the time and effort to respond to my question.
0
 tab=[1,2,3,4,5]
    function new_splice(index,arr){
        for(i=0;i<arr.length-1;i++)
            if(i==index-1){
                for(j=i;j<arr.length-1;j++)
                    arr[j]=arr[j+1]
            }
            new_arr=[];
            for(i=0;i<arr.length-1;i++)
                new_arr[i]=arr[i]
        return new_arr;
    }
    document.write(new_splice(1,tab))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.