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

297
Visualizações
How to remove duplicate names in an object with multiple arrays?

I have the object below with different arrays and I want to remove the duplicate names that are showing. How can I do that?

How duplicate names look like

The data is being printed this way

All data

The dsUserId is what is being repeated. And I want to be able to remove duplicate dsUserId

Here is where all the data is being generated.

  this.setState({
      user_connections: results.data.data.connections,
      newLeads: results2.data.data,
      promotion: results3.data.data,
      hasPromotion: results3.data.data.length === 0 ? false : true,
      promoPlan: promoPlan,
      toRemoveDays: excludedDays
    });

    console.log("All data", this.state.user_connections)
  }

And here is where the data is being rendered.

  <select
    id="user_select"
    style={{ width: "70%" }}
    name="selectedUser"
    onChange={e => {
      this.setState({
        [e.target.name]: e.target.value.trim()
      });
    }}
    className="form-control"
  >
    <option value={null} disabled hidden selected>
      Please Select
    </option>
    {this.state.user_connections.map(u => (
      <option value={u.parentId} key={u.parentId}>
        {u.displayName}
      </option>
    ))}
  </select>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use filter to achieve this for eg:

const userConnections = (connections) => {
    const uniqueIds = [];
    return connections.filter(connection => {
       if (!uniquesIds.includes(connection.dsUserId) {
           uniqueIds.push(connection.dsUserId);
           return connection;
       }
    })
};

and then :

user_connections: userConnections(results.data.data.connections)
about 4 years ago · Juan Pablo Isaza Relatório

0

Not sure if the best way, but I would first make a Set of all the unique dsUserIds and then filter the first in the user_connections array for each dsUserId:

let tsUserIds = [];
this.state.user_connections.forEach(user => {
  tsUserIds.push(user.dsUserId);
})
tsUserIds = [...new Set(tsUserIds)]; // filters out the duplicates
let uniqueConnections = [];
tsUserIds.forEach(id => {
  uniqueConnections.push(this.state.user_connections.filter(user => user.dsUserId === id)[0]);
})
console.log(uniqueConnections);
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