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

242
Visualizações
Remove duplicates from a nested array of objects

I'm trying to remove duplicates from a nested array of objects.

let arr = [{
  type: "horizontal",
  tiles: [{ col: 0, row: 3 }, { col: 1, row: 3 }, { col: 2, row: 3 }]
},
{
  type: "horizontal",
  tiles: [{ col: 1, row: 3 }, { col: 2, row: 3 }, { col: 0, row: 3 }]
},
{
  type: "horizontal",
  tiles: [{ col: 2, row: 3 }, { col: 1, row: 3 }, { col: 0, row: 3 }],
},
{
  type: "vertical",
  tiles: [{ col: 0, row: 5 }, { col: 0, row: 3 }, { col: 0, row: 4 }],
}];

I'm trying to only keep the top level objects if they are unique. As you can see the tiles array isn't necessarily sorted by col/row.

So arr here would become:

let arr = [{
  type: "horizontal",
  tiles: [{ col: 0, row: 3 }, { col: 1, row: 3 }, { col: 2, row: 3 }]
},{
  type: "vertical",
  tiles: [{ col: 0, row: 5 }, { col: 0, row: 3 }, { col: 0, row: 4 }],
}];

I've looked extensively but can't quite find something that solves the same problem I'm trying to solve.

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

0

Okay this may be a little long but I couldn't find a shorter way of doing. My plan was to get a Set of JSON strings (which gives unique) and to convert it back to array of JS objects (Set of JS objects won't remove duplicates). This way will get only the unique values.

But for the JSON strings to be equal for unique records the tiles array has to be in same order so I ran sort on tiles first to make sure the order is same

let arr = [{
  type: "horizontal",
  tiles: [{ col: 0, row: 3 }, { col: 1, row: 3 }, { col: 2, row: 3 }]
},{
  type: "horizontal",
  tiles: [{ col: 1, row: 3 }, { col: 2, row: 3 }, { col: 0, row: 3 }]
},{
  type: "horizontal",
  tiles: [{ col: 2, row: 3 }, { col: 1, row: 3 }, { col: 0, row: 3 }],
},{
  type: "vertical",
  tiles: [{ col: 0, row: 5 }, { col: 0, row: 3 }, { col: 0, row: 4 }],
}];

//sort for ascending order of col field
arr.forEach((o,i,a) => {
    a[i].tiles.sort((a,b) => a.col-b.col)
})


let mySet = new Set();
arr.forEach(item => mySet.add(JSON.stringify(item)))

let result = []
mySet.forEach((el) => {
    result.push(JSON.parse(el))
})


console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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