Stuart Brown
Stuart Brown

Reputation: 987

Create an object from an array with same key, value values

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:

  1. The key and value are the same, however
  2. 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?

Upvotes: 1

Views: 132

Answers (2)

Majed Badawi
Majed Badawi

Reputation: 28414

You can use .reduce:

const 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",
   }
];

const newarray= arr.reduce((acc,{variation}) => {
  acc[variation.replace(/\s/g, '')] = variation;
  return acc;
}, {});

console.log(newarray);

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370759

Once you have the variation string, map it to an object by removing the spaces to construct the key and pass it to Object.fromEntries:

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",
  }
];
const deduplicatedVariations = [...new Set(arr.map((o) => o.variation))];
const result = Object.fromEntries(
  deduplicatedVariations.map(
    str => [str.replaceAll(' ', ''), str]
  )
);
console.log(result);

Upvotes: 2

Related Questions