如何统计嵌套数组中detail.id或groupDocs.id的唯一值?
统计唯一的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);
完整代码(含原始数据、分组逻辑及无效尝试)
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:从原始data中统计唯一detail.id
利用flatMap扁平化所有details数组,提取每个detail.id后存入Set自动去重,最后通过size属性获取数量:
const uniqueDetailIds = new Set(data.flatMap(item => item.details.map(detail => detail.id))); console.log("唯一detail.id数量:", uniqueDetailIds.size); // 输出:3
方法2:从groupDocs中统计唯一id
注意groupDocs中的id可能是单个数字或数组(当原details包含多个元素时),需要先将数组展开,再存入Set:
const uniqueGroupIds = new Set( groupDocs.flatMap(item => Array.isArray(item.id) ? item.id : [item.id]) ); console.log("唯一groupDocs.id数量:", uniqueGroupIds.size); // 输出:3
无效尝试的问题分析
- v1错误原因:
data.map(i => i.details.id)中i.details是数组,没有id属性,最终得到的是一组undefined,无法统计有效id。 - v2错误原因:直接将
groupDocs中的对象存入Set,每个对象都是独立引用,Set无法识别重复的id值,只能判断对象引用是否相同。 - v3错误原因:用整个
detail对象作为统计键,对象会被转为字符串[object Object],所有统计项都会被归为同一个键,最终统计的是总数量而非唯一id数量。
内容的提问来源于stack exchange,提问作者user1128912
相关产品推荐
相关产品推荐

