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

279
Visualizações
How to remove duplicate entries from array of arrays based on nested value?

I have an array of arrays that also contains hash value , and that array has duplicate arrays that I want to remove. There is an example below :

[[“John, Doe“, "/manager/consumer/123456?status=1", {:data=>{:id=>123456},  :class=>""}],
[“Jane, smith“, "/manager/consumer/7891011?status=1", {:data=>{:id=>7891011},  :class=>""}],
[“William, Smith”, "/manager/consumer/12131415?status=1", {:data=>{:id=>1211415}, :class=>""}],
[“John, Doe“, "/manager/consumer/123456?status=1", {:data=>{:id=>123456}, :class=>""}]]

As we can see there are two arrays that are same(first and last), I would like to remove the duplicate one based on the :id present in the hash inside the array ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use uniq method! (ruby >= 1.9.2)

array = [
  ["John, Doe", "/manager/consumer/123456?status=1", {:data=>{:id=>123456},  :class=>""}],
  ["Jane, smith", "/manager/consumer/7891011?status=1", {:data=>{:id=>7891011},  :class=>""}],
  ["William, Smith", "/manager/consumer/12131415?status=1", {:data=>{:id=>1211415}, :class=>""}],
  ["John, Doe", "/manager/consumer/123456?status=1", {:data=>{:id=>123456}, :class=>""}]
]

array.uniq { |_name, _url, hash| hash[:data][:id] }

In case of duplicate of an id it will remove all but the first entry, so you need to think about a situation when the id is the same but rest of the data is not.

NOTE: if you for some reason are running on ruby before 1.9.2, then uniq will ignore the block. For that reason ActiveSupport had uniq_by method (which was removed in version 4.0.2).

over 4 years ago · Santiago Trujillo 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