Use CaseSo our lead programmer loves to follow the NIH Anti-Pattern and consequently I'm not allowed to use Underscore.js and can only use Vanilla JS... and we have to support IE8.The goal is to produce an aggregation of unique values from nested collections buried within various collections of objects.Desired InterfaceThe idea is to call a function to create an aggregation function give the list of the nested properties.ExampleThe following would flatten all options collection that are buried under each object within the properties property.var allUniqueOptions = aggregateByProperty('properties', 'options');var allUniqueDoodadsOptions = aggregateByProperty('doodad', 'properties', 'options');allUniqueOptions(myCollection);allUniqueDoodadsOptions(myDoodadCollection);Current CodeThe code below works, but I'd like any advice on improving it for:performancereadabilityany further generalizationI have a jsFiddle created./* Returns the index of an object within a collection */function arrayObjectIndexOf(arr, obj) {    var search = JSON.stringify(obj);    for ( var i = 0, k = arr.length; i < k; i++ ){        if (JSON.stringify(arr[i]) == search) {            return i;        }    };    return -1;};/* Concatentates objects that don't exist in the source collection */function concatIfNotExists(source, additions) {    var result = source;    for ( var i = 0, k = additions.length; i < k; i++ ) {        var addition = additions[i];        if (arrayObjectIndexOf(result, addition) === -1) {            result.push(addition);        }    };    return result;};/* Returns a nested collection buried within an object */function getInnerCollection(obj, props) {    var innerCollection = obj;    for (var j = 0, l = props.length; j < l; j++) {        if (innerCollection.hasOwnProperty(props[j])) {            innerCollection = innerCollection[props[j]];        } else {            throw new TypeError(Property doesn't exist!);        }    }    if (!innerCollection instanceof Array) {        throw new TypeError(Inner property is not a collection!);    }    return innerCollection;};/* Returns an aggregation of objects based on collection properties */function aggregateByProperty() {    var props = Array.prototype.splice.call(arguments, 0);    return function(objectCollection) {        var result = [];        for (var i = 0, k = objectCollection.length; i < k; i++) {            var innerCollection = getInnerCollection(objectCollection[i], props);            result = concatIfNotExists(result, innerCollection);        }        return result;    }};
Aggregation of a collection of object's nested array-properties
javascript;functional programming
From a once over : This is a problem : var search = JSON.stringify(obj); because JSON.stringify() does not guarantee any order.. To be completely correct I think you are going to need to pass the properties to arrayObjectIndexOf that need to get compared ( that is, if uniqueness is determined by only comparing the provided properties, otherwise you are in a world of trouble )Inner property is not a collection! <- Perhaps props[l-1] +  is not an array!, then you will provide which property is not an array, and be clear that you are expecting an arrayProperty doesn't exist! <- Property  + props[j] + does not exist will give a better feedback as to which part is not workingJSHint.com could only find semicolon issues, which is good