瞄准
我想计算有多少个detail.id
或groupDocs.id
是唯一的。我可以通过初始的data
(detail.id
)或通过groupDocs
(groupDocs.id
)的结果来计算。这两个都在底部的代码片段中。
"我试过的事都不管用"
const v1 = new Set(data.map(i => i.details.id))
console.log("v1: ", v1);
const v2 = [...new Set(groupDocs)];
console.log("v2: ", v2);
const v3 = data.reduce((total, el) => {
el.details.forEach((el) => {
if (el in total) total[el]++;
else total[el] = 1;
});
return total;
}, {});
console.log("v3: ", v3);
以下是包含原始data
、数组操作groupDocs
和我的失败尝试的代码片段
const data = [
{
id: 222222,
document_file_name: "4020653_FileName.pdf",
document_url: "www.",
document_updated_at: "2020-07-08T19:41:28.385Z",
document_type_label: "doc label",
state: "accepted",
details: [
{
id: 20656,
type: "Claimant",
name: "First Name Last Name",
first_name: "First Name",
last_name: "Last Name",
type_label: "claimant"
},
{
id: 20657,
type: "Fellow",
name: "Fellow First Name Fellow Last Name",
first_name: "Fellow First Name",
last_name: "Fellow Last Name",
type_label: "fellow"
}
]
},
{
id: 1111111,
document_file_name: "4020600_FileName.pdf",
document_url: "www.",
document_updated_at: "2020-07-08T19:41:28.385Z",
document_type_label: "doc label",
state: "rejected",
details: [
{
id: 2000,
type: "Fellow",
name: "Fellow First Name Fellow Last Name",
first_name: "Fellow First Name",
last_name: "Fellow Last Name",
type_label: "fellow"
},
{
id: 20656,
type: "Claimant",
name: "First Name Last Name",
first_name: "First Name",
last_name: "Last Name",
type_label: "claimant"
}
]
},
{
id: 333333,
document_file_name: "4020890_FileName.pdf",
document_url: "www.",
document_updated_at: "2020-07-08T19:41:28.385Z",
document_type_label: "doc label",
state: "rejected",
details: [
{
id: 20657,
type: "Fellow",
name: "Fellow First Name Fellow Last Name",
first_name: "Fellow First Name",
last_name: "Fellow Last Name",
type_label: "fellow"
}
]
},
{
id: 444444,
document_file_name: "4020672_FileName.pdf",
document_url: "www.",
document_updated_at: "2020-07-08T19:41:28.385Z",
document_type_label: "doc label",
state: "rejected",
details: [
{
id: 20657,
type: "Fellow",
name: "Other",
first_name: "First Name",
last_name: "Last Name",
type_label: "claimant"
}
]
}
];
const groupDocs = [
...data
.reduce(
(
travelDocsMap,
{
id: travel_doc_id,
document_file_name,
document_type_label,
document_url,
state,
details = []
}
) => {
let details_ids = details.map((o) => o.id).sort();
const details_ids_unique = details_ids.join("_"),
type = details.map((o) => o.type),
name = details.map((o) => o.name);
if (details_ids.length === 1) {
details_ids = details_ids[0];
}
const details_g = travelDocsMap.get(details_ids_unique) ?? {
id: details_ids,
type,
name,
docs: []
};
details_g.docs.push({
travel_doc_id,
document_file_name,
document_type_label,
document_url,
state
});
travelDocsMap.set(details_ids_unique, details_g);
return travelDocsMap;
},
new Map()
)
.values()
];
console.log("groupDocs: ", groupDocs);
const v1 = new Set(data.map(i => i.details.id))
console.log("v1: ", v1);
const v2 = [...new Set(groupDocs)];
console.log("v2: ", v2);
const v3 = data.reduce((total, el) => {
el.details.forEach((el) => {
if (el in total) total[el]++;
else total[el] = 1;
});
return total;
}, {});
console.log("v3: ", v3);
如果你愿意帮忙的话
1条答案
按热度按时间xqnpmsa81#
你试过这个吗?
阅读更多here。