Вам нужен другой подход, взяв хеш-таблицу и комбинированный ключ для доступа.
var array = [{ id: "1", category: "cat1", "question1": "blue", "question2": "blue" }, { id: "1", category: "cat1", "question1": "blue", "question2": "red" }, { id: "2", category: "cat2", "question1": "blue", "question2": "blue" }, { id: "2", category: "cat2", "question1": "red", "question2": "blue" }],
result = Object.values(array.reduce((r, o) => {
['question1', 'question2'].forEach(question => {
var key = [o.category, question].join('|');
r[key] = r[key] || { category: o.category, question, blue: 0, red: 0 };
r[key][o[question]]++;
});
return r;
}, Object.create(null)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
С динамическими вопросами (синий / красный):
var array = [{ id: "1", category: "cat1", "question1": "blue", "question2": "blue" }, { id: "1", category: "cat1", "question1": "blue", "question2": "red" }, { id: "2", category: "cat2", "question1": "blue", "question2": "blue" }, { id: "2", category: "cat2", "question1": "red", "question2": "blue" }],
questions = new Set,
result = Object.values(array.reduce((r, o) => {
['question1', 'question2'].forEach(question => {
var key = [o.category, question].join('|');
if (!questions.has(o[question])) {
Object.values(r).forEach(p => p[o[question]] = 0);
questions.add(o[question]);
}
r[key] = r[key] || Object.assign({ category: o.category, question }, ...Array.from(questions, q => ({ [q]: 0 })));
r[key][o[question]]++;
});
return r;
}, Object.create(null)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }