DECISION TO MY ORIGINAL QUESTION
Thanks to Dan's brilliance below, I was able to significantly reduce the source code. I gave up on some of the flexibility I originally created, i.e. Used several properties vs static prop, but an easy fix in a later version and no problem in terms of code size. Again, many thanks to Dan. In this example, I have included 2 forget() methods wrapped in the Angular Collection factory.
Collection.$inject = ['Constructor', 'Article']; function Collection(Constructor, Article) { var Model = Article, Collection = {}, collection = [], add, forget; function Extendable(data, keys) { Constructor.call(this, data, Model.fillable); } Extendable.prototype = Object.create(Constructor.prototype); Extendable.prototype.constructor = Extendable; Collection = Extendable.prototype; collection = Collection.items = []; add = function(item) { if (item instanceof Model) { collection.push(item); } }; Collection.add = add;
DEV TOOLS
Angular 1.4.8, lodash 2.x
Question:
I am still Angular and JS. Is there a more concise approach to deleting multiple instances of objects (for example, articles / s in the collection of articles). The forget() method takes 1 argument, which may contain the string || integer || array (strings or integers)
Example:
var articleIdsToRemove = [1, '2', 3];
OR
var articleIdsToRemove = 1; var articles = newCollection();
For brevity, I omitted the add method to add article instances to the collection, but suppose we have 10, each with id support
MY CURRENT APPROACH
- check the type of the value, as indicated above, the array, str, int and set all the values ββto the array from int, i.e.
ids - then iterate over the array of identifiers to find the index for
Article that has prop id and is equal to id - if found, click index on indexes. We have all the relevant indexes then send it to
Array.prototype.remove to remove multiple elements from the collection array. Once this is done, re-sort the collection.
the Angular factory shell has been removed for brevity, but suppose all of this is in an Angular factory called Collection, which introduces the factory article, which is a data model. Briefly Collection (articles) contains a lot of Article
// We set a collections array inside of object to hold all Article instances collection = Extendable.prototype.items = []; // NOTE add articles method removed, but assume weve added multiple Article to collection forget = function(id) { var ids = [], index = null, indices = []; if (angular.isDefined(id) && !_.isEmpty(id)) { ids = _.isArray(id) ? id : isInt(id) ? [Number(id)] : []; if (ids.length) { _.each(ids, function(id) { index = getIndex('app_id', id); if (index) { indices.push(index) } }); if (indices.length) { collection = collection.remove(indices) } } } }; Extendable.prototype.forget = forget; function isInt(n){ return Number(n) === n && n % 1 === 0; } function getIndex(prop, value) { return _.indexOf(collection, function(d) { if (hasProp(d, prop)) { return d[prop] == value; } }); } function hasProp (obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); } Array.prototype.remove = function(){ var args = Array.apply(null, arguments); var indices = []; for(var i = 0; i < args.length; i++){ var arg = args[i]; var index = this.indexOf(arg); while(index > -1){ indices.push(index); index = this.indexOf(arg, index + 1); } } indices.sort(); for(var i = 0; i < indices.length; i++){ var index = indices[i] - i; this.splice(index, 1); } };