Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!