javascript - 计算出现次数并创建对象
问题描述
我有一个这样的数组
var myArray = [
{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"}
];
我想计算每个问题的结果出现次数,并用这样的新对象创建新数组
var newArray = [
{category: "cat1", question:"question1", blue:2, red:0},
{category: "cat1", question:"question2", blue:1, red:1},
{category: "cat2", question:"question1", blue:1, red:1},
{category: "cat2", question:"question2", blue:2, red:0}
]
我尝试使用此功能
var result = myArray.reduce((r, {
category,
question1,
question2
}) => {
r.push({
category,
question1
}, {
category,
question2
})
return r;
}, [])
这是一个片段:
var myArray = [
{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"}
];
var result = myArray.reduce((r, {
category,
question1,
question2
}) => {
r.push({
category,
question1
}, {
category,
question2
})
return r;
}, [])
console.log(result);
解决方案
您需要一种不同的方法,采用哈希表和组合键进行访问。
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; }
推荐阅读
- rest - AWS ALB 后面的微服务(ish)设置中的健康检查路由组织
- typescript - TypeScript 中的“复合类型”
- sql-server - 如何将多月数据放入多列
- git - 使用 Visual Studio 代码在 Visual Studio Team Server(或 gitlab)上创建远程 git 存储库
- c++ - 在排序的文本文件中实现二进制搜索?
- angularjs - 使用 ng-options 和 'as' 设置标签('track by' 替代方案)
- python - 从 Sage 中提取单一解决方案
- javascript - React 中状态更新时动态更改获取请求 url
- events - Google 表格 onEdit 事件不起作用
- android - google sheet自定义菜单功能在android上的google sheet中不可见