I have a JavaScript array which has the following contents:
var products = [
{category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},
{category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
{category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
{category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
{category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
{category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
];
I want to count the number of unique entries in each key.
For example,
For the key
categorythere are 2 unique entries.For the key
namethere are 5 unique entries.
I tried using Object.keys(products).length, but that only gives the total number of rows in the array.
How can I get the counts?