I want to edit the label text so that whenever there are duplicates of the same label name, they are incremented so that they could be differentiated when displayed.
So the input data looks below:
const brandName = [
{
id: '1',
label: 'Nike'
},
{
id: '2',
label: 'Adidas'
},
{
id: '3'
label: 'Nike'
}
]
And the output data of the function should display this:
const newBrandName = [
{
id: '1',
label: 'Nike (1)'
},
{
id: '2',
label: 'Adidas'
},
{
id: '3'
label: 'Nike (2)'
}
]
Here is my attempt:
function findDuplicates(brandName){
var l = 0
var r = tournaments.length - 1
var count = 1
while(r < tournaments.length){
if(tournaments[l].label === tournaments[r].label){
tournaments[l].label += count
count += 1
tournaments[r].label += count
}
r -= 1
}
}
Is there a way I could use a JavaScript inbuilt array function?