这些JS数组中重复的项,如何合并成一个长尾词?
- 内容介绍
- 文章标签
- 相关推荐
本文共计284个文字,预计阅读时间需要2分钟。
输入数据:[{description: , fieldId: 882783718376513532, id: 890850722614423552, identifier: 敬仰, name: 敬仰, typeIdentifier: 主日类, typeName: 主日类, value: , valueType: }, {description: , fieldId: 88278, identifier: 数据, name: 数据, typeIdentifier: 主日类, typeName: 主日类, value: , valueType: }]
输入数据[ { "description":"", "fieldId":"882783718376513532", "id":"890850722614423552", "identifier":"敬拜", "name":"敬拜", "typeIdentifier":"主日类", "typeName":"主日类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"890860636057452544", "identifier":"带小组", "name":"带小组", "typeIdentifier":"周间类", "typeName":"周间类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"894213656757518336", "identifier":"夜来香", "name":"夜来香", "typeIdentifier":"主日d", "typeName":"主日类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"894756803174469633", "identifier":"aa", "name":"aa", "typeIdentifier":"周间类", "typeName":"周间类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"895117403331354625", "identifier":"a", "name":"a硕士", "typeIdentifier":"主日类", "typeName":"主日类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"895150266944667650", "identifier":"u", "name":"u方法", "typeIdentifier":"主日类", "typeName":"主日类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"895157599322062849", "identifier":"lin", "name":"linsf", "typeIdentifier":"主日类", "typeName":"主日类", "value":"", "valueType":"" } ] 处理逻辑
const areas = {}; const serviceAreas = []; data.map((item) => { const index = item.typeName; if (index in areas) { areas[index] = areas[index] + "|" + item.name; let result = serviceAreas.find((n) => n.key === index); result.value.push(item.name); } else { const result = { key: index, value: [item.name] }; serviceAreas.push(result); areas[index] = item.name; } }) console.log(serviceAreas); 返回结果
[ { "key": "主日类", "value": [ "敬拜", "夜来香", "a硕士", "u方法", "linsf" ] }, { "key": "周间类", "value": [ "带小组", "aa" ] } ]
本文共计284个文字,预计阅读时间需要2分钟。
输入数据:[{description: , fieldId: 882783718376513532, id: 890850722614423552, identifier: 敬仰, name: 敬仰, typeIdentifier: 主日类, typeName: 主日类, value: , valueType: }, {description: , fieldId: 88278, identifier: 数据, name: 数据, typeIdentifier: 主日类, typeName: 主日类, value: , valueType: }]
输入数据[ { "description":"", "fieldId":"882783718376513532", "id":"890850722614423552", "identifier":"敬拜", "name":"敬拜", "typeIdentifier":"主日类", "typeName":"主日类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"890860636057452544", "identifier":"带小组", "name":"带小组", "typeIdentifier":"周间类", "typeName":"周间类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"894213656757518336", "identifier":"夜来香", "name":"夜来香", "typeIdentifier":"主日d", "typeName":"主日类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"894756803174469633", "identifier":"aa", "name":"aa", "typeIdentifier":"周间类", "typeName":"周间类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"895117403331354625", "identifier":"a", "name":"a硕士", "typeIdentifier":"主日类", "typeName":"主日类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"895150266944667650", "identifier":"u", "name":"u方法", "typeIdentifier":"主日类", "typeName":"主日类", "value":"", "valueType":"" }, { "description":"", "fieldId":"882783718376513532", "id":"895157599322062849", "identifier":"lin", "name":"linsf", "typeIdentifier":"主日类", "typeName":"主日类", "value":"", "valueType":"" } ] 处理逻辑
const areas = {}; const serviceAreas = []; data.map((item) => { const index = item.typeName; if (index in areas) { areas[index] = areas[index] + "|" + item.name; let result = serviceAreas.find((n) => n.key === index); result.value.push(item.name); } else { const result = { key: index, value: [item.name] }; serviceAreas.push(result); areas[index] = item.name; } }) console.log(serviceAreas); 返回结果
[ { "key": "主日类", "value": [ "敬拜", "夜来香", "a硕士", "u方法", "linsf" ] }, { "key": "周间类", "value": [ "带小组", "aa" ] } ]

