Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
Filtering out multiple && duplicate values in an array

I'm struggling to find a clean way to reduce or filter an array to contain no duplicate set values, when 2 of the values match. Here is an example of the data I am using. I am trying to remove any duplicates where title && genre both match.

[
  {
    "title": "american-hustle",
    "genre": "arts",
    "user": "penny"
  },
  {
    "title": "american-hustle",
    "genre": "comedy",
    "user": "brian"
  },
  {
    "title": "platoon",
    "genre": "war",
    "user": "tom"
  },
  {
    "title": "american-hustle",
    "genre": "arts",
    "user": "sarah"
  },
  {
    "title": "american-hustle",
    "genre": "arts",
    "user": "john"
  }
]

So in this case, the final two items should be removed, as both title && genre match that of an existing entry. Note, the second item should remain as american-hustle with a genre comedy, is still unique.

I've tried to find a similar question but I'm struggling to find one. Any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Presented below is one possible way to achieve the desired objective.

Code Snippet

const myTransform = arr => (
  Object.values(
    arr.reduce(
      (acc, {title, genre, ...rest}) => (
        acc[`${title}${genre}`] ??= {title, genre, ...rest},
        acc
      ),
      {}
    )
  )
);
// explanation of code is below
/* transform array to remove dupes
const myTransform = arr => (
  Object.values(      // extract only the values of the below intermediate result-object
    arr.reduce(       // use ".reduce()" to iterate with "acc" as accumulator
      // de-structure the iterator to access title, genre, other props
      (acc, {title, genre, ...rest}) => (
        // conditionally assign value to 'acc' with key as combination of
        // title=genre and value as the original object's key-value pairs
        acc[`${title}${genre}`] ??= {title, genre, ...rest},
        // NOTE: Using "??=" ensures the first occurance of a dupe is retained
        // If one would use "=" instead, the last occurance shall be retained
        // always return "acc"
        acc
      ),
      {}        // initialize "acc" as an empty object
    )
  )             // implicit return of the object-values array
);
*/
const myArr = [
  {
    "title": "american-hustle",
    "genre": "arts",
    "user": "penny"
  },
  {
    "title": "american-hustle",
    "genre": "comedy",
    "user": "brian"
  },
  {
    "title": "platoon",
    "genre": "war",
    "user": "tom"
  },
  {
    "title": "american-hustle",
    "genre": "arts",
    "user": "sarah"
  },
  {
    "title": "american-hustle",
    "genre": "arts",
    "user": "john"
  }
];

console.log(
  'removed dupes using title-genre combination',
  myTransform(myArr)
);
.as-console-wrapper { max-height: 100% !important; top: 0 }

Explanation

Inline comments added to the snippet above.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda