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

160
Visualizações
Find all urls for each array value

Given the following array structure:

{ url: "www.site.com", ids: ["F20", "C10", "C05"] }, { url: "www.site.com/something", ids: ["F20", "C06", "C05"] }, { url: "www.site.com/somethingelse", ids: ["F21", "C12", "C05"] }

I’m trying to loop over the array and find all urls for each id. So the end result would group the urls where an id value has the same url:

F20 for instance would have www.site.com and www.site.com/something.

The data structure would end up being an array of objects where each id would have an array of urls in which the id exists.

I have started with a basic for loop, but stuck on the best way to loop over and check the url for each value of the array it’s associated with and then group by id.

for(id of ids){ console.log(id) }

The end result would be a list of ids with all associated urls where that id appears.

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

0

You can loop over input array and then loop over ids for each input element and keep adding urls for corresponding ids. For e.g.

const input = [{ url: "www.site.com", ids: ["F20", "C10", "C05"] }, { url: "www.site.com/something", ids: ["F20", "C06", "C05"] }, { url: "www.site.com/somethingelse", ids: ["F21", "C12", "C05"] }]
const output = {}
input.forEach(obj=>{
    const ids = obj.ids
    if(ids && ids.length>0){
        ids.forEach(id=>{
            if(!output[id]){
                output[id] = []
            }
            output[id].push(obj.url)
        })
    }
})

for(let id of Object.keys(output)){
    console.log(`${id}:${JSON.stringify(output[id])}`)
}
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