Skip to content

Commit ec4f9d9

Browse files
authored
Create renameDuplicates.js
1 parent 4fc48fc commit ec4f9d9

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 1
2+
var array = [
3+
{
4+
name: "Steven Smith",
5+
Country: "England",
6+
Age: 35
7+
},
8+
{
9+
name: "Hannah Reed",
10+
Country: "Scottland",
11+
Age: 23
12+
},
13+
{
14+
name: "Steven Smith",
15+
Country: "Spain",
16+
Age: 35
17+
},
18+
];
19+
20+
let names = {};
21+
arr.forEach(obj => {
22+
if (names[obj.name]) {
23+
obj.name += "_" + ++names[obj.name];
24+
} else {
25+
names[obj.name] = 1;
26+
}
27+
});
28+
29+
30+
// 2
31+
function renameFiles(arr) {
32+
let count = {};
33+
arr.forEach((x, i) => {
34+
if (arr.indexOf(x) !== i) {
35+
let c = x in count ? (count[x] = count[x] + 1) : (count[x] = 1);
36+
let j = c + 1;
37+
let k = `${x }(${ j })`;
38+
39+
while (arr.indexOf(k) !== -1) k = `${x }(${ ++j })`;
40+
arr[i] = k;
41+
}
42+
});
43+
return arr;
44+
}
45+
46+
let res = renameFiles(['a(1)', 'a(6)', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']);
47+
console.log(res);

0 commit comments

Comments
 (0)