Where can I find a List class for javascript with add(obj) remove(obj) and contains(obj)?
Asked
Active
Viewed 1,750 times
4 Answers
2
you can already use push instead of add and pop instead of remove.
How do I check if an array includes an object in JavaScript? contains an answer for how to do contains.
2
You could write some simple array extensions:
Array.prototype.add = function (obj) {
if (this && this.length)
this.push(obj);
};
[1, 2, 3].add(4);
... etc

Josiah Ruddell
- 29,697
- 8
- 65
- 67
1
Those aren't hard features to implement. I would simply create my own custom type that inherited from Array and added the two additional methods you'd like (since you can already use push
to add items).

Justin Niessner
- 242,243
- 40
- 408
- 536
1
You can just use the native javascript's arrays
var arr = [];
arr.push(5); // add(obj)
arr.indexOf(5); // returns the index in the array or -1 if not found -> contains(obj)
For removing, you can use arr.pop()
to remove last element or arr.shift()
to remove the first element.
More on javascript's arrays - http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

Radoslav Georgiev
- 1,366
- 8
- 15
-
+1 for answer. Wish I could give another +1 for that link. Thanks! – Ed Bayiates Feb 24 '12 at 20:39