Working with the following array of objects:
const data = [
{count: 400, value: "Car Wash Drops"},
{count: 48, value: "Personal/Seeding"},
{count: 48, value: "Personal/Seeding"},
];
I want to use the map
function to create an array with an additional identifier for duplicate values:
const expected = [
["Car Wash Drops", 400],
["Personal/Seeding (1)", 48],
["Personal/Seeding (2)", 48],
];
I currently have a map function that maps the values correctly, but I'm unsure how to add the identifier only for duplicates.
data.map(d => [`${d.value}`, d.count]);
This results in:
[
["Car Wash Drops", 400],
["Personal/Seeding", 48],
["Personal/Seeding", 48],
]
I also tried using the index, but it adds the index to every value:
data.map((d, i) => [`${d.value} ${i}`, d.count]);
This results in:
[
["Car Wash Drops (0)", 400],
["Personal/Seeding (1)", 48],
["Personal/Seeding (2)", 48],
]