Reputation: 562
I'm trying to wrap my mind around a problem I need solved, but I don't seem to get anywhere. What I want done is most likely easier to exemplify.
I have an array of objects, looking 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, what I want to achieve is to turn this array into 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 a MapReduce function (from Underscore.js) to solve this, but after failing to generate the output I wanted, and with the same (failed) result from a few other for-tries, I decided to ask here. Maybe someone knows what must be done?
And, sorry for the horrible title. If you've got a better idea for it, please comment (might help others reach this question as well)
Upvotes: 4
Views: 113
Reputation: 156562
My solution uses "reduce" and "map" to compute the counts and transform the data:
function countVisitDates(visits) {
// Count the visits per city per date using "reduce".
var counts = visits.reduce(function(memo, visit) {
var cityName=visit.city_name, visitDate=visit.visited;
if (!memo[cityName]) { memo[cityName] = {}; }
memo[cityName][visitDate] |= 0;
memo[cityName][visitDate] += 1;
return memo;
}, {});
// Transform the count structure using "map".
return Object.keys(counts).map(function(cityName) {
return {
key: city_name,
values: Object.keys(counts[cityName]).map(function(date) {
return [date, counts[cityName][date]];
})
};
});
};
countVisitDates(data);
/* =>
[
{
"key": "New York",
"values": [
[ "2014-10-20", 3 ],
[ "2014-10-21", 2 ]
]
},
{
"key": "Stockholm",
"values": [
[ "2014-10-20", 2 ],
[ "2014-10-21", 2 ]
]
}
]
*/
Upvotes: 1
Reputation:
here you go:
function make_it_so(src)
{
var usd={}, rsl=[], idx=-1;
src.forEach(function(val,key,arr)
{
var cty = val["city_name"];
if (!usd[cty])
{
idx++;
usd[cty] = {};
rsl[idx] = {"key":cty, "values":[]};
}
if (!usd[cty][val["visited"]])
{
usd[cty][val["visited"]] = val["visited"];
rsl[idx]["values"][rsl[idx]["values"].length] = val["visited"];
}
});
return rsl;
}
var src = [
{"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"}
];
console.log(make_it_so(src));
Upvotes: 2
Reputation: 144709
If using Underscore is an option:
var result = _.chain(arr).groupBy('city_name').map(function (el, key) {
return {
key: key,
values: _.chain(el).countBy('visited').pairs().value()
}
}).value();
Upvotes: 3
Reputation: 173642
You can create an intermediate data structure to keep track of both countries and visits to each country; then you map that structure into the final output:
var tmp = {};
visits.forEach(function(item) {
var obj = tmp[item.city_name] || (tmp[item.city_name] = {});
obj[item.visited] = (obj[item.visited] || 0) + 1;
});
var result = Object.keys(tmp).map(function(key) {
return {
key: key,
values: Object.keys(tmp[key]).map(function(date) {
return [date, tmp[key][date]];
})
};
});
Upvotes: 4
Reputation: 53666
You could just walk the data:
var data = [
{"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"},
];
var newdata = {};
data.forEach(function(entry) {
var city = entry.city_name;
var time = entry.visited;
if(!newdata[city]) {
newdata[city] = {};
}
if(!newdata[city][time]) {
newdata[city][time]=0;
}
newdata[city][time]++;
});
This gives an equivalence structure that can then be transformed as needed using Object.keys()
:
{
"New York": {
"2014-10-20": 3,
"2014-10-21": 2
}
,
...
}
Upvotes: 0