I'm trying to think about a problem that I need to solve, but it seems like I'm not going anywhere. What I want to do is most likely easier to illustrate.
I have an array of objects that looks like this:
[
{"city_name": "New York", "visited": "2014-10-20"},
{"city_name": "New York", "visited": "2014-10-20"},
{"city_name": "New York", "visited": "2014-10-20"},
{"city_name": "New York", "visited": "2014-10-21"},
{"city_name": "New York", "visited": "2014-10-21"},
{"city_name": "Stockholm", "visited": "2014-10-20"},
{"city_name": "Stockholm", "visited": "2014-10-20"},
{"city_name": "Stockholm", "visited": "2014-10-21"},
{"city_name": "Stockholm", "visited": "2014-10-21"},
]
Now I want this array to include the following:
[
{
"key": "New York",
"values": [
['2014-10-20', 3], // Because there were 3 visits in New York at this date
['2014-10-21', 2] // Because there were 2 visits in New York at this date
]
},
{
"key": "Stockholm",
"values": [
['2014-10-20', 2],
['2014-10-21', 2]
]
}
]
I tried using the MapReduce function (from Underscore.js) to solve this problem, but after I could not generate the desired result, and with the same (unsuccessful) result of several other attempts, I decided to ask here, Maybe someone Does anyone know what needs to be done?
And, sorry for the terrible title. If you have a better idea for this, please comment (may help others reach this point as well)