在嵌套对象 nodejs | 中按 ID 查找用户详细信息javascript

Find user details by ID in nested object nodejs | javascript

如何使用 id 查找姓名。表示迭代对象。创建一个函数 const searchName =()=>{} 假设如果在函数中传递 3,那么我想显示....这样的用户名是什么

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" },
    ],
}
]

假设用户传递 3 所以我想 return { "id": 3, "name": "c" } 这样。 我正在尝试迭代它并通过 id 找到用户的名字,但我不理解这个迭代所以我需要你的帮助。

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
}

我遍历键,我只是跳过“服务”一个,因为它不相关。 然后,我过滤“serviceN”数组,它将return一个对象数组(如果找到只有一个,如果找不到则为空数组)。

如果找到,我们将停止迭代。

然后我们 return 第一个(也是逻辑上唯一的元素)或者如果没有找到则为 null

您可以使用 flat and find 的组合来通过 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

检查此代码....输入任何身份证号码

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)