查询重复数组中重复元素个数并将重复元素名称添加(1)(

tech2022-08-19  114

``

let arr = [{ name: "A", url: "xxxx" }, { name: "A", url: "xxxx" }, { name: "A", url: "xxxx" }, { name: "A.doc", url: "xxxx" }, { name: "A", url: "xxxx" }, { name: "C", url: "xxxx" }, { name: "V", url: "xxxx" }, { name: "B", url: "xxxx" }, { name: "C", url: "xxxx" }, { name: "C", url: "xxxx" }, { name: "A", url: "xxxx" }] function handleDuplicateNames(target, key) { let obj = {} //利用对象键名唯一性,查出元素出现的个数 target.forEach(item => { var name = item[key]; if (obj[name]) { console.log() item[key] = `${item[key]}(${obj[name]})`; obj[name] = obj[name] + 1; } else { obj[name] = 1; } }) } handleDuplicateNames(arr, 'name')
最新回复(0)