I am facing a challenge with an array that contains duplicate values. I aim to transform this array into an object that only includes unique values, using the array values as both key and value.
In the example provided below, I have successfully created an object with only unique values. However, I am struggling to achieve an object format similar to:
{
RookieTicketVariationRPS: "Rookie Ticket Variation RPS",
VeteranTicketVariationRP: "Veteran Ticket Variation RPS",
OpticsSeasonTicketRed: "Optics Season Ticket Red"
}
The notable differences in this desired format are:
- The key and value are identical,
- All whitespace has been removed from each 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)
If anyone can offer guidance on how to achieve this specific outcome, it would be greatly appreciated.