gpt4 book ai didi

javascript - 使用 Underscore js 过滤、分组和聚合数据

转载 作者:行者123 更新时间:2023-11-30 15:49:51 24 4
gpt4 key购买 nike

我有这样的原始数据:

var data = {
"issues": [
{
"fields": {
"project": {
"key": "ProjectA"
},
"components": [
{
"name": "Component A"
},
{
"name": "Component C"
}
],
"priority": {
"name": "P0"
},
"status": {
"name": "Closed"
}
}
},
{
"fields": {
"project": {
"key": "ProjectA"
},
"components": [
{
"name": "Component B"
}
],
"priority": {
"name": "P1"
},
"status": {
"name": "Reopened"
}
}
},
{
"fields": {
"project": {
"key": "ProjectA"
},
"components": [
{
"name": "Component B"
}
],
"priority": {
"name": "P1"
},
"status": {
"name": "Closed"
}
}
},
{
"fields": {
"project": {
"key": "Project B"
},
"components": [
{
"name": "Component X"
}
],
"priority": {
"name": "P1"
},
"status": {
"name": "Closed"
}
}
}
]
};

如果我将 ProjectA 作为输入,我想按如下方式过滤、分组和聚合输出:

"components": [
{
"name": "Component A",
"priorities": [
{
"name": "P0",
"status": [
{
"name": "Open",
"count": 0
},
{
"name": "Reopened",
"count": 0
},
{
"name": "Closed",
"count": 1
}
]
},
{
"name": "P1",
"status": [
{
"name": "Open",
"count": 0
},
{
"name": "Reopened",
"count": 0
},
{
"name": "Closed",
"count": 0
}
]
}
]
},
{
"name": "Component B",
"priorities": [
{
"name": "P0",
"status": [
{
"name": "Open",
"count": 0
},
{
"name": "Reopened",
"count": 0
},
{
"name": "Closed",
"count": 0
}
]
},
{
"name": "P1",
"status": [
{
"name": "Open",
"count": 0
},
{
"name": "Reopened",
"count": 1
},
{
"name": "Closed",
"count": 1
}
]
}
]
},
{
"name": "Component C",
"priorities": [
{
"name": "P0",
"status": [
{
"name": "Open",
"count": 0
},
{
"name": "Reopened",
"count": 0
},
{
"name": "Closed",
"count": 1
}
]
},
{
"name": "P1",
"status": [
{
"name": "Open",
"count": 0
},
{
"name": "Reopened",
"count": 0
},
{
"name": "Closed",
"count": 0
}
]
}
]
}
]

我试过链接、过滤、分组,但不确定如何使用这些,因为我的数据太嵌套了。

最佳答案

这是一个纯 javascript 提案,首先收集所有组件和优先级,然后构建一个结果对象,然后增加状态计数。

var data = { "issues": [{ "fields": { "project": { "key": "ProjectA" }, "components": [{ "name": "Component A" }, { "name": "Component C" }], "priority": { "name": "P0" }, "status": { "name": "Closed" } } }, { "fields": { "project": { "key": "ProjectA" }, "components": [{ "name": "Component B" }], "priority": { "name": "P1" }, "status": { "name": "Reopened" } } }, { "fields": { "project": { "key": "ProjectA" }, "components": [{ "name": "Component B" }], "priority": { "name": "P1" }, "status": { "name": "Closed" } } }, { "fields": { "project": { "key": "Project B" }, "components": [{ "name": "Component X" }], "priority": { "name": "P1" }, "status": { "name": "Closed" } } }] },
componentsO = Object.create(null),
prioritiesO = Object.create(null),
components,
priorities,
status_ = ['Open', 'Reopened', 'Closed'], // status collides in chrome with window.status
hash = Object.create(null),
getKey = function (a) { return a.join('|'); },
result = {};

data.issues.forEach(function (issue) {
if (issue.fields.project.key === 'ProjectA') {
issue.fields.components.forEach(function (component) {
componentsO[component.name] = true;
});
prioritiesO[issue.fields.priority.name] = true;
}
});

components = Object.keys(componentsO).sort();
priorities = Object.keys(prioritiesO).sort();

result.components = components.map(function (component) {
return {
name: component,
priorities: priorities.map(function (priority) {
return {
name: priority,
status: status_.map(function (status) {
var key = getKey([component, priority, status]);
hash[key] = { name: status, count: 0 };
return hash[key];
})
};
})
}
});

data.issues.forEach(function (issue) {
if (issue.fields.project.key === 'ProjectA') {
issue.fields.components.forEach(function (component) {
var key = getKey([component.name, issue.fields.priority.name, issue.fields.status.name]);
hash[key].count++;
});
}
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 使用 Underscore js 过滤、分组和聚合数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39571804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com