Home > Enterprise >  Find user details by ID in nested object nodejs | javascript
Find user details by ID in nested object nodejs | javascript

Time:03-28

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.

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

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
  • Related