1

I had this code suggested to me:

var activityArray = [];
activityArray.push("Loading content 1");
activityArray.push("Loading content 2");
activityArray.push("Loading content 3");
activityArray.push("Loading content 4");

//find the item we want to delete
var index = activityArray.indexOf('Loading content 4');// returns 3
activityArray.splice(index,1)//remove the item at index 3

I have a lot of places where I want to add elements to my array so "activityArray.push" seems like a very easy way to do this.

I also have a lot of places where I want to remove the elements from the array. Is there a way that I could take the code to delete the item and add it as a function to activityArray so that I could say:

activityArray.pull("Loading content 4"); 
1
  • Perhaps you're asking "how do I extend the Array type in javascript?" This might give you good contextual information: stackoverflow.com/questions/8859828/… Commented Dec 1, 2013 at 9:50

4 Answers 4

3

You can add methods to the array prototype for instance specific methods.

Array.prototype.pull = function(content) {
    var index = this.indexOf(content);
    this.splice(index, 1);
};

var activityArray = [];
activityArray.push("Loading content 1");
activityArray.push("Loading content 2");
activityArray.push("Loading content 3");
activityArray.push("Loading content 4");

activityArray.pull("Loading content 4"); 

>> ["Loading content 1", "Loading content 2", "Loading content 3"]
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way I can apply it to just the activityArray object ?
See my updated answer for a version that works on a single array.
joews - Did you change anything. The answer looks the same?
2

Yes, Javascript's prototypical inheritance system makes it very easy to monkey patch new functionality onto existing objects.

This version, like your code, will return the first instance of the removed element, or undefined if the element did not exist in the array.

Array.prototype.pull = function(element) { 
  var index = this.indexOf(element); 
  var removed = undefined;
  if(index >= 0)
    removed = this.splice(index,1); 
  return removed;
}

If you just want to add the method to a single array,

myArray = [1,2,3,4,5,6];

myArray.pull = function(element) { 
  var index = this.indexOf(element); 
  var removed = undefined;
  if(index >= 0)
    removed = this.splice(index,1); 
  return removed;
}

myArray.pull(3) // -> [1,2,4,5,6]
[3,2,1].pull(2) // -> exception

3 Comments

var removed = undefined; is excessive. var removed; will be enough.
That is true, but there is no harm in being explicit - it makes the functionality more obvious to a novice.
@Alan FYI: prototype meddling is not recommended (especially using the name pull) as this might someday be used as an actual prototype method and cause you some serious headaches.
2

If you can skip using a simple Array try a named array \ object syntax:

var activityArray = {};

activityArray["Loading content 1"] = "Loading content 1";
activityArray["Loading content 2"] = "Loading content 2";
activityArray["Loading content 3"] = "Loading content 3";
activityArray["Loading content 4"] = "Loading content 4";

// To remove simply use the `delete` keyword
delete activityArray["Loading content 4"];

1 Comment

This is a good answer provided that each element can exist only once in the data structure. Given that the other answers, and the OP's code, will only remove the first instance of a given element anyway, that is a pretty good assumption to make.
0

Expanding on the answer from @joews. In order not to violate the standard Array class, I redefined it and then extended my class:

function initNewArray(arr) {
    class MyArray extends Array {
        pull(element) {
            const index = this.indexOf(element);
            let removed = undefined;
            if (index >= 0)
                removed = this.splice(index, 1);
            return removed;
        }
    }

    return new MyArray(...arr);
}

const myArr = initNewArray([1, 2, 3, 4, 5, 6]);

myArr.pull(3) // -> [1,2,4,5,6]
[3, 2, 1].pull(2) // -> exception

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.