Reputation: 167
I have an array like this:
var data = [
{ Group: 'A', Name: 'SD' },
{ Group: 'B', Name: 'FI' },
{ Group: 'A', Name: 'MM' },
{ Group: 'B', Name: 'CO' }
];
I want to get only the unique Group values in an array like:
var unique = ['A','B'];
I looked at some of the examples on SO but I don't understand them. Can anyone tell me how I should do this?
Upvotes: 1
Views: 1444
Reputation: 89527
If you are using ES6/ES2015 or later you can do it this way:
var unique = [...new Set(data.map(item => item.Group))];
Here is an example on how to do it.
Upvotes: 0
Reputation: 93020
var data = [
{ Group: 'A', Name: 'SD' },
{ Group: 'B', Name: 'FI' },
{ Group: 'A', Name: 'MM' },
{ Group: 'B', Name: 'CO' }
];
var set = {};
for (var i = 0; i < data.length; i++)
set[data[i].Group] = 1;
var arr = [];
for(var key in set)
arr.push(key);
alert(arr);
Upvotes: 2