1

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?

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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);

1 Comment

many thanks for your answer, it works perfectly but I'm marking the first answer as 'the' answer since it was first.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.