Skip to content

@alexguzman /UniquePropertyValues.js
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Finds Unique Values for Provided Properties on an Object Array
var Cars = [
{ Make: 'BMW', Model: '850ci', Color: 'Green' },
{ Make: 'Rover', Model: 'SD1', Color: 'Black' },
{ Make: 'VM', Model: 'Golf', Color: 'Bronze' },
{ Make: 'Volvo', Model: 'P1800', Color: 'Green' },
{ Make: 'BMW', Model: '850ci', Color: 'Green' },
{ Make: 'Rover', Model: 'SD1', Color: 'Black' },
{ Make: 'VM', Model: 'Golf', Color: 'Bronze' },
{ Make: 'Volvo', Model: 'P1800', Color: 'Green' }
];
/**
* Finds Unique Values for Provided Properties
* @param {Array.<Object>} Objects
* @param {Array.<string>} Properties
* @return {Array.<Array>} Unique Values for Properties
*/
var UniquePropertyValues = function(Objects, Properties) {
var UniqueProperties = [];
for(var i = Objects.length - 1; i >= 0; i--) {
for(var j = 0, l = Properties.length; j < l; j++) {
if(UniqueProperties[j] == void 0)
UniqueProperties[j] = [];
if(
Objects[i][Properties[j]] != void 0 &&
UniqueProperties[j].indexOf(Objects[i][Properties[j]]) == -1
)
UniqueProperties[j].push(Objects[i][Properties[j]]);
}
}
return UniqueProperties;
};
console.log(UniquePropertyValues(Cars, ['Make', 'Model', 'Color']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.