const
//get the answers as an array
answers = Array.prototype.slice.call(document.querySelectorAll('.answers td')).map(function (a) { return a.innerText; }),
//get the questions as an array
questions = Array.prototype.slice.call(document.querySelectorAll('.questions th')),
//get the length of the questions
qLen = questions.length,
//create a dictionary object
dict = {};
let arr;
//loop the questions array
questions.forEach(function (q, i) {
//set the var arr to an array of answers matching the column of the question
arr = answers.filter(function (a, ai) { return (ai - i) % qLen == 0; });
//set the dictionary line to an empty object
dict[q.innerText] = {};
arr
//uniquify the answers
.filter(function (x, i) { return arr.indexOf(x) === i; })
//loop the uniquified answers to set the dictionary.question = the answer
//then set the dictionary.question.answer = count
.forEach(function (x) { dict[q.innerText][x] = arr.filter(function(a) { return a == x; }).length; });
});
//result is a dictionary that can be drilled down into a count via dict.question.answer
console.log(dict);
1条答案
按热度按时间lb3vh1jj1#
使用下面的vanilla JS看起来能满足你的需求。如果你能控制的话,使用不同的html结构会简单得多。但是这会给你一个原型来工作和重构以满足你的需求。