javascript - Underscore.js 如何计数

标签 javascript underscore.js javascript-objects

我是 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/

相关文章:

javascript - 删除索引处的单个 child

javascript - 为数组的每一项添加额外的属性

javascript - 使用对象符号向对象添加方法

Javascript:滚动到光标在 contenteditable div 中粘贴

javascript - JS : If variable gets object from a function, 变量是否存储对象的内容或对象的链接?

javascript - 如何按子值将对象数组拆分为多个对象数组

javascript - 返回空对象的箭头函数然后返回一个函数?

javascript - 转换 javascript 对象列表

javascript - 未捕获的类型错误 : undefined is not a function (jQuery & jQuery Easing)

javascript - Leafletjs - 数组中存在传单点