我正在试图解决我需要解决的问题,但我似乎无处可去。我想做的事情很可能更容易举例说明。
我有一个对象数组,如下所示:
[
{"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"},
]
现在,我想要实现的是将此数组转换为以下内容:
[
{
"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]
]
}
]
我尝试使用MapReduce函数(来自Underscore.js)来解决这个问题,但是在无法生成我想要的输出后,以及其他一些尝试的相同(失败)结果,我决定在这里问一下。也许有人知道必须做些什么?
对不起这个可怕的头衔。如果您对此有更好的了解,请发表评论(也可以帮助其他人解决此问题)
答案 0 :(得分:4)
您可以创建一个中间数据结构来跟踪每个国家和每个国家/地区的访问量;然后将该结构映射到最终输出:
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]];
})
};
});
答案 1 :(得分:3)
如果使用Underscore是一个选项:
var result = _.chain(arr).groupBy('city_name').map(function (el, key) {
return {
key: key,
values: _.chain(el).countBy('visited').pairs().value()
}
}).value();
答案 2 :(得分:2)
你去:
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));
答案 3 :(得分:1)
我的解决方案使用"减少"和"地图"计算计数并转换数据:
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 ]
]
}
]
*/
答案 4 :(得分:0)
你可以走数据:
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]++;
});
这给出了一个等价结构,然后可以使用Object.keys()
:
{
"New York": {
"2014-10-20": 3,
"2014-10-21": 2
}
,
...
}