I have an array that has duplicate values which I want to turn into an object which contains only unique values, and uses the values in the array as both key and value.
In my example below I can create an object containing only unique values but I can't figure out how to get to an object that instead of
{
0: "Rookie Ticket Variation RPS",
1: "Veteran Ticket Variation RPS",
2: "Optics Season Ticket Red"
}
looks like
{
RookieTicketVariationRPS: "Rookie Ticket Variation RPS",
VeteranTicketVariationRP: "Veteran Ticket Variation RPS",
OpticsSeasonTicketRed: "Optics Season Ticket Red"
}
The difference here is that:
- The key and value are the same, however
- whitespace has been removed from the the string
let arr = [
{
"manufacturer":"Panini",
"brand":"Contenders",
"variation":"Rookie Ticket Variation RPS",
},
{
"manufacturer":"Panini",
"brand":"Contenders",
"variation":"Veteran Ticket Variation RPS",
},
{
"manufacturer":"Panini",
"brand":"Contenders",
"variation":"Rookie Ticket Variation RPS",
},
{
"manufacturer":"Panini",
"brand":"Contenders",
"variation":"Optics Season Ticket Red",
}
]
let set = [...new Set(arr.map((o) => o.variation))]
let newarray= { ...set}
console.log(newarray)
Can anyone suggest the correct way to do this?