+ 2
How transform array1 from to JavaScript
[xxx,yyy,aaa,bbb,xxx,xxx,aaa]=>[xxx1,yyy,aaa1,bbb,xxx2,xxx3,aaa2]
12 Answers
+ 6
Here's my try ...
https://code.sololearn.com/W50yM268dG4u/?ref=app
+ 4
Alessio Farroni Assuming you are talking about the transform of the array
['xxx','yyy','aaa','bbb','xxx','xxx','aaa']
to unique element with running number
Here the solution
https://code.sololearn.com/cZb9Kdpy78oD/?ref=app
+ 3
Alessio Farroni
Please add more information to understand your problem.
+ 3
Something like this ?
a=["xxx","yyy","aaa","bbb","xxx","xxx","aaa"]
d={}
a.forEach((item,index)=>{
if(item!="bbb"){
if(!(item in d)){
d[item]=1
a[index]=item+1
}
else{
a[index]=item+(1+d[item])
d[item]+=1
}
}
})
console.log(a)
+ 3
Alessio Farroni doesn't matter . The solution i gave you should work in every case given you understand what is going on .
+ 2
Alessio Farroni
Nice question and challenging too.
0
//I thik you want to remove duplicate elements from array.
var array1 = ['a','b','b','c','a','c','c'];
var array2 = [...new Set(array1)];
console.log(array2); // expected output: a,b,c
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ i start from an Array with strig if a string Is unique my Array return the string without change else return the string with a progressive Number perhaps if in the array i have text, text return in new Array text1,text2
0
Abhay thanks for your answer, the problem Is the array Is dinamicaly and i don't know if in the array i have bbb or cc or another value
0
Abhay ok, i'll try...thank you very much
0
Calviղ thanks at the Moment i have create this but i think Is a Better way create new Array ti push the value
var a=["xxx","yyy","aaa","bbb","xxx","xxx","aaa",'ccc']
var counter = [];
function checkDupli (){
var lunghezza = a.length;
var dupli= [];
for(var i = 0; i < lunghezza; i++){ // itero tutti i valori dell'array
for(var j =i+1; j< lunghezza; j++){ // itero tutti i valori dell'array con loro stessi
if(a[i]== a[j] && i != j ){
dupli.push(a[j]);
counter[a[j]] = 1;
a[j]= a[j] + counter[a[j]]
counter[a[j]] = counter[a[j]] +1;
}else{a[i]= a[i]}
}
}
console.log(dupli);
}
checkDupli();
console.log(a)
Ho quasi vinto