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

148
Visualizações
How can I get objects of Set type variable unique?

I am working on a scraping script and found about Set object which should store unique datas and fast in performance. So I tried it like this

let scrapedMessages = new Set()

scrapedMessages.add({
                text,
                ...(images.length > 0 && {
                  images,
                }),
                senderID,
                timestamp,
              })

But when looking at the data scraped I found out these type of duplicate datas

  {
    "text": "Acne Fighting Facial Wash With Jojoba Beads",
    "senderID": "361571627329333",
    "timestamp": "1613017270619"
  },
  {
    "text": "Acne Fighting Facial Wash With Jojoba Beads",
    "senderID": "361571627329333",
    "timestamp": "1613017270619"
  }

Does it mean Set of Objects might not be unique or I am doing some mistakes. I was doing it through simple array. But shifted to Set type for better performance. Can it be achieved? What is the best practice?

I am running the puppeteer script on Nodejs.

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

0

JS use === operator to compare the new element to all others which already exists in the set and based on that it decides to either add the new element or ignore it.

The problem is, objects are address of memory:

const a = {x: 1};
const b = {x: 1};
const c = a;

console.log(a===b); // false
console.log(a===c); // true
console.log(b===c); // false

So here there are couple of things you can do:

  1. Extend the Set class and override the add method to compare the object's properties instead of using ====.

  2. NOT RECOMMENDED but you can simply use JSON.stringify(object) before adding it.

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