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

113
Visualizações
Find user details by ID in nested object nodejs | javascript

How to find name using id. means iterate object. create a function const searchName =()=>{} suppose if pass 3 in function so I'd want to show .... what the name of user like this

const data = [{
    "service": [
        "BUSINESS",
        "LEGAL",
        "FINANCE",
        "ADVERTISEMENT"
    ],
    "service1": [
        { "id": 1, "name": "a" },
        { "id": 2, "name": "b" },
        { "id": 3, "name": "c" },
        { "id": 4, "name": "d" },
    ],
    "service2": [
        { "id": 5, "name": "e" },
        { "id": 6, "name": "f" },
        { "id": 7, "name": "g" },
        { "id": 8, "name": "h" },
    ],
    "service3": [
        { "id": 9, "name": "i" },
        { "id": 10, "name": "j" },
        { "id": 11, "name": "k" },
        { "id": 12, "name": "l" },
    ],
    "service4": [
        { "id": 13, "name": "m" },
        { "id": 14, "name": "n" },
        { "id": 15, "name": "o" },
        { "id": 16, "name": "p" },
    ],
}
]

suppose user pass 3 so I want to return { "id": 3, "name": "c" } like this. I'm trying to iterate this and find the name of the user by id but I didn't understand this iteration so I need your help.

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

0

check this code.... Enter any id number

const data = [{
                    "service": [
                        "BUSINESS",
                        "LEGAL",
                        "FINANCE",
                        "ADVERTISEMENT"
                    ],
                    "service1": [
                        { "id": 1, "name": "a" },
                        { "id": 2, "name": "b" },
                        { "id": 3, "name": "c" },
                        { "id": 4, "name": "d" },
                    ],
                    "service2": [
                        { "id": 5, "name": "e" },
                        { "id": 6, "name": "f" },
                        { "id": 7, "name": "g" },
                        { "id": 8, "name": "h" },
                    ],
                    "service3": [
                        { "id": 9, "name": "i" },
                        { "id": 10, "name": "j" },
                        { "id": 11, "name": "k" },
                        { "id": 12, "name": "l" },
                    ],
                    "service4": [
                        { "id": 13, "name": "m" },
                        { "id": 14, "name": "n" },
                        { "id": 15, "name": "o" },
                        { "id": 16, "name": "p" },
                    ],
                }]
            var itemobj = ''
            const searchName =(val)=>{
                    console.log('searchname')
                    data.map((item)=>{
                        let obj = Object.keys(item)            
                        obj.map((data)=>{
                            let inrdata = item[data]
                            inrdata.map((initem)=>{                    
                                 let lastdata = initem.id===val?itemobj=initem:null  
                                  
                                
                            })

                        })
                    })
                }
                
            searchName(3)
             console.log(itemobj)

about 4 years ago · Juan Pablo Isaza Relatório

0

function searchName(id) {
let result = null;
  for (const [key, value] of Object.entries(data)) {
    if (key === "service") continue
    result = value.filter(obj => {
        return obj.id === id
        })
    if (result) break
  }
  return result ? result[0] : null
}

I iterate through keys, I just skip "service" one since it's not revelant. Then, I filter the "serviceN" array, it will return an array of object (only one if found, empty array if not found).

If it's found, we stop iterating.

Then we return either the first (and logically only element) or null if not found

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use a combination of flat and find to get get the user by id

function searchName(id) {
  return data
    .flatMap((item) => Object.values(item))
    .flat()
    .find((user) => user.id === id);
}

const result = searchName(3); // { id: 3, name: 'c' } | undefined
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