我是 underscore.js 库的新手,也是 javascript 的新手。我已经成功地使用 countBy 方法来总结我的回复。但是你如何使用 countBy 来计算不存在的东西与一些预定义的桶?或者我应该完全使用不同的方法吗?
考虑以下示例,这里是 JSFiddle http://jsfiddle.net/itchesavvy/bfmhtp5e/2/
var data = [{
questionData: {
type: 'multiple choice',
content: {
text: 'This is the question?',
answers: [{
_id: 'id1',
value: 'First answer'
}, {
_id: 'id2',
value: 'Second answer'
}, {
_id: 'id3',
value: 'Third answer'
}, {
_id: 'id4',
value: 'Fourth answer'
}]
}
},
responses: [{
answer: 'id1',
timestamp: 1
}, {
answer: 'id2',
timestamp: 4
}, {
answer: 'id1',
timestamp: 10
}, {
answer: 'id3',
timestamp: 15
}, {
answer: 'id3',
timestamp: 16
}, {
answer: 'id1',
timestamp: 25
}, {
answer: 'id2',
timestamp: 35
}, {
answer: 'id3',
timestamp: 42
}, {
answer: 'id1',
timestamp: 44
}, {
answer: 'id1',
timestamp: 50
}, {
answer: 'id2',
timestamp: 70
}, {
answer: 'id3',
timestamp: 80
}, {
answer: 'id2',
timestamp: 81
}]
}];
var b = _.countBy(data[0].responses,'answer');
console.log(b);
这会产生以下对象 {id1:5, id2:4, id3:4}
我想知道如何使用来自 questionData.content.answers 的数据获取 {id1:5, id2:4, id3:4, id4:0}
更重要的是,我想知道如何获得 {'第一个答案':5, '第二个答案':4, '第三个答案':4, '第四个答案':0}
可能再次在值字段中使用来自 questionData.content.answers 的数据。
最佳答案
不确定您是否需要使用下划线。
var data=[{questionData:{type:"multiple choice",content:{text:"This is the question?",answers:[{_id:"id1",value:"First answer"},{_id:"id2",value:"Second answer"},{_id:"id3",value:"Third answer"},{_id:"id4",value:"Fourth answer"}]},responses:[{answer:"id1",timestamp:1},{answer:"id2",timestamp:4},{answer:"id1",timestamp:10},{answer:"id3",timestamp:15},{answer:"id3",timestamp:16},{answer:"id1",timestamp:25},{answer:"id2",timestamp:35},{answer:"id3",timestamp:42},{answer:"id1",timestamp:44},{answer:"id1",timestamp:50},{answer:"id2",timestamp:70},{answer:"id3",timestamp:80},{answer:"id2",timestamp:81}]}}];
//Simplify your object traversal
var questionData = data[0].questionData;
responses = questionData.responses,
content = questionData.content,
//Stage your counters
i = 0,
l = content.answers.length,
//Will hold your answers array as key value pairs for easier lookups
answers = {},
//Will hold the final results
results = {};
//Iterate over your answers collection
for(; i < l; i++){
//The value of each answer will be used as keys to look up counts in our results object
var key = content.answers[i].value;
//We will cross reference 'id#' values with the results keys
answers[content.answers[i]._id] = key;
//Start each possible result with a count of zero.
results[key] = 0;
}
//Output our answers (cross reference object)
console.log(answers);
//Reset our counters to the responses collection
i = 0;
l = responses.length;
//Iterate over the responses
for(; i < l; i++){
//Use the answer within each response to look up the appropriate results key from our answers object.
var key = answers[responses[i].answer];
//Increment the result value.
results[key]++;
}
//Output the counts of each answer.
console.log(results);
关于javascript - Underscore.js 如何计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26518538/