我有一个如下所示的数组。
var inputData = [
{label:1, time: Mon},
{label:1, time: Tue},
{label:2, time: Wed},
{label:2, time: Mon},
{label:2, time: Thu},
{label:3, time: Tue},
{label:3, time: Mon},
{label:3, time: Mon},
{label:3, time: Thu},
{label:1, time: Fri},
......
];
标签从1到3,时间是从星期一到星期五。我想计算标签和时间的每个组合的数量。最后,我想要一个像这样的数组:
var newArray = [
{label:1, time:Mon, count:19},
{label:1, time:Tue, count:34},
{label:1, time:Wed, count:36},
{label:1, time:Thu, count:21},
{label:1, time:Fri, count:32},
{label:2, time:Mon, count:55},
{label:2, time:Tue, count:25},
{label:2, time:Wed, count:87},
{label:2, time:Thu, count:44},
{label:2, time:Fri, count:56},
......
];
这是我尝试的但不幸的是它不起作用。有人可以帮忙吗?欣赏!
var labelArr = d3.map(inputData, function(d) {return d['label'];}).keys();
//labelArr = ["1","2","3"];
var filteredTime = d3.map(inputData, function(d) {return d['time'];}).keys();
//filteredTime = ["Mon", "Tue","Wed","Thu","Fri"];
var newArray = [];
for(var i = 0;i < filteredTime.length; i++){
var count = 0;
for(var j = 0; j < inputData.length; j++){
if(inputData[j]['time'] === filteredTime[i]){
for(var s = 0; s < labelArr.length; s++){
if(inputData[j]['label'] === labelArr[s]){
count++;
}
}
}
}
newArray.push({
'label':labelArr[s],
'time': filteredTime[i],
'count':count
})
}
答案 0 :(得分:2)
以下是您的代码稍作重新安排: -
var labelArr = d3.map(inputData, function(d) {
return d['label'];
}).keys();
var filteredTime = d3.map(inputData, function(d) {
return d['time'];
}).keys();
var newArray = [];
for (var i = 0; i < filteredTime.length; i++) {
for (var s = 0; s < labelArr.length; s++) {
var count = 0;
for (var j = 0; j < inputData.length; j++) {
if (inputData[j]['time'] == filteredTime[i] && inputData[j]['label'] == labelArr[s])
count++;
}
newArray.push({
'label': labelArr[s],
'time': filteredTime[i],
'count': count
});
}
}
您的主要问题是.keys
返回标签的字符串数组。然后===
也比较了返回false(int === string
)的类型。
答案 1 :(得分:1)
另一种方法可能是使用对象作为地图来计算外观。可以将...
# Since we have custom UserAdmin, we need to do special things for django-hijack
# See here: http://django-hijack.readthedocs.org/en/latest/troubleshooting/
from hijack.admin import HijackUserAdminMixin
admin.site.unregister(User)
class CustomUserAdminWithHijackButton(HijackUserAdminMixin, CustomUserAdmin):
"""
We are subclassing HijackUserAdminMixin to display the hijack button in the admin.
"""
list_display = CustomUserAdmin.list_display + ('hijack_field', )
admin.site.register(User, CustomUserAdminWithHijackButton)
和label
连接起来,形成一个用于计算不同值的人工密钥。
time
完成计数后,可以通过d3.values()
var map = {};
inputData.forEach(function(d) {
var key = "" + d.label + d.time;
if (!map[key]) { // if the key is not yet in the map...
d.count = 0; // add a count property to the object
map[key] = d; // and add the object to the map
}
map[key].count++ // increase object's counter
});
请查看此代码段以获取有效示例:
var newArray = d3.values(map);
"use strict";
var inputData = [{
label: 1,
time: "Mon"
}, {
label: 1,
time: "Tue"
}, {
label: 2,
time: "Wed"
}, {
label: 2,
time: "Mon"
}, {
label: 2,
time: "Thu"
}, {
label: 3,
time: "Tue"
}, {
label: 3,
time: "Mon"
}, {
label: 3,
time: "Mon"
}, {
label: 3,
time: "Thu"
}, {
label: 1,
time: "Fri"
}, ];
var map = {};
inputData.forEach(function(d) {
var key = "" + d.label + d.time;
if (!map[key]) { // if the key is not yet in the map...
d.count = 0; // add a count property to the object
map[key] = d; // and add the object to the map
}
map[key].count++ // increase object's counter
});
var newArray = d3.values(map); // we are only interested in the array of values
//////// Just beautifying output below...
d3.select("p").append("text")
.text(JSON.stringify(newArray, null, " "));
p {
white-space: pre;
font-family: monospace;
}