I have the following object that I'm receiving from an API:
{
'2012-12-12': [
{ 'id': 1234,
'type': 'A' },
{ 'id': 1235,
'type': 'A' },
{ 'id': 1236,
'type': 'B' },
],
'2012-12-13': [
{ 'id': 1237,
'type': 'A' },
{ 'id': 1238,
'type': 'C' },
{ 'id': 1239,
'type': 'B' },
]
}
Then I want to have another variable named types of type Array that will hold every possible value of the type attribute of each one of the objects. In this case it would be:
types = ['A', 'B', 'C']
I'm trying to have it done in a functional way (I'm using underscore.js) but I'm unable to figure out a way of doing it. Right now I'm using
types = [];
_.each(response, function(arr1, key1) {
_.each(arr1, function(arr2, key2) {
types.push(arr2.type);
});
});
types = _.uniq(types);
But that's very ugly. Can you help me in figuring out a better way of writing this code?
Thanks!
{}). Then you can use it as a way to keep track of types you've already seen.arr2as it's actually anobjectin this case, but that's more developer empathy than functional improvement.