Skip to content

Commit 5384212

Browse files
authored
Update renameDuplicates.js
1 parent ec4f9d9 commit 5384212

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

js-coding-technique/renameDuplicates.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,134 @@ var array = [
4545

4646
let res = renameFiles(['a(1)', 'a(6)', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']);
4747
console.log(res);
48+
49+
50+
// Other should update
51+
function renameFiles(arrObj, renameField='contents') {
52+
let arr = [];
53+
if (renameField === 'contents') {
54+
arrObj.forEach((child) => {
55+
arr.push(child.properties[renameField]);
56+
});
57+
} else {
58+
arrObj.forEach((child) => {
59+
arr.push(child.properties.attributes[renameField]);
60+
});
61+
}
62+
63+
let count = {};
64+
arr.forEach((x, i) => {
65+
if (arr.indexOf(x) !== i) {
66+
let c = x in count ? (count[x] = count[x] + 1) : (count[x] = 1);
67+
let j = c + 1;
68+
let k = `${x} (${j})`;
69+
70+
while (arr.indexOf(k) !== -1) k = `${x} (${++j})`;
71+
arr[i] = k;
72+
}
73+
});
74+
75+
for (let i = 0; i < arrObj.length; i++) {
76+
arrObj[i].properties.contents = arr[i];
77+
}
78+
79+
return arrObj;
80+
}
81+
82+
// let res = renameFiles(['a(1)', 'a(6)', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']);
83+
let res = renameFiles([
84+
{
85+
id: 1,
86+
properties: { contents: 'a' },
87+
},
88+
{
89+
id: 2,
90+
properties: { contents: 'a' },
91+
},
92+
{
93+
id: 3,
94+
properties: { contents: 'a' },
95+
},
96+
{
97+
id: 1,
98+
properties: { contents: 'a' },
99+
},
100+
{
101+
id: 1,
102+
properties: { contents: 'a (3)' },
103+
},
104+
]);
105+
console.log(res);
106+
107+
// function renameDuplicates(arr) {
108+
// let count = {};
109+
// arr.forEach((x, i) => {
110+
// if (arr.indexOf(x) !== i) {
111+
// console.log(arr.indexOf(x), i);
112+
// let c = x in count ? (count[x] = count[x] + 1) : (count[x] = 1);
113+
// let j = c + 1;
114+
// let k = `${x}(${j})`;
115+
116+
// while (arr.indexOf(k) !== -1) k = `${x}(${++j})`;
117+
// arr[i] = k;
118+
// }
119+
// });
120+
// return arr;
121+
// }
122+
123+
// let res1 = renameDuplicates(['a(1)', 'a(6)', 'a(1)', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']);
124+
// console.log(res1);
125+
126+
const renameDuplicates = (childrens) => {
127+
// console.log({ inputChildrens: childrens });
128+
let temp = childrens.length;
129+
130+
childrens = JSON.parse(JSON.stringify(childrens));
131+
let count = {};
132+
childrens.forEach((child) => {
133+
if (count[child.properties.contents]) {
134+
child.properties.contents += ` (${++count[child.properties.contents]})`;
135+
while (temp) {
136+
temp--;
137+
if (childrens[temp].properties.contents === child.properties.contents || count[child.properties.contents])
138+
child.properties.contents += ` (${1})`;
139+
}
140+
} else {
141+
count[child.properties.contents] = 1;
142+
}
143+
144+
// if (count[child.properties.attributes.value]) {
145+
// child.properties.attributes.value += ` (${++count[child.properties.attributes.value]})`;
146+
// } else {
147+
// count[child.properties.attributes.value] = 1;
148+
// }
149+
});
150+
151+
return childrens;
152+
};
153+
154+
const res1 = renameDuplicates([
155+
{
156+
id: 1,
157+
properties: { contents: 'a' },
158+
},
159+
{
160+
id: 2,
161+
properties: { contents: 'a' },
162+
},
163+
{
164+
id: 3,
165+
properties: { contents: 'a' },
166+
},
167+
{
168+
id: 1,
169+
properties: { contents: 'a (2)' },
170+
},
171+
{
172+
id: 1,
173+
properties: { contents: 'a (3)' },
174+
},
175+
]);
176+
177+
// console.log(res1);
178+

0 commit comments

Comments
 (0)