JSFiddle - React, Tailwind, and code Playground
by dswitzer
JavaScript
// a generic collection to track items
function Collection(items){
this.items = items || [];
}
Collection.prototype.get = function(i){
return this.items[i];
}
Collection.prototype.getAll = function(i){
return this.items;
}
Collection.prototype.count = function(o){
return this.items.length;
}
Collection.prototype.add = function(o){
this.items.push(o);
}
Collection.prototype.remove = function(o){
// find the object and remove
for( var i=0; i < this.count(); i++ ){
// we found the item to unregister
if( o === this.items[i] ){
this.items.splice(i, 1);
return true;
}
}
return false;
}
var Items = new Collection();
console.log(Items.count());
Items.add("1");
Items.add("2");
Items.add("3");
console.log(Items.count());
console.log(Items.getAll());
Items.remove("2");
console.log(Items.count());
console.log(Items.getAll());