js或者es6 已知id为222的对象数据,如何查询并返回该数据中parentId: 7对应的对象父节点数据的对象

要查询并返回id为222的对象数据中parentId为7对应的对象的父节点数据,我们需要遍历整个data数组,找到id为222的对象,并从其父节点中提取信息。由于您提供的data数组中的对象格式存在问题(例如,对象的键值对应该用花括号{}包围),我将先帮您修正格式,然后提供查询的代码。

修正后的data数组格式如下:

javascript 复制代码
const data = [
    {
        "id": 1,
        "name": "常用指标",
        "parentId": 0,
        "signalTypeIds": "",
        "harmonicTimes": "",
        "children": [
            {
                "id": 7,
                "name": "相电压",
                "parentId": 1,
                "signalTypeIds": {
                    "0": 1,
                    "1": 94,
                    "2": 161,
                    "3": 228,
                    "4": 295
                },
                "harmonicTimes": "",
                "children": [
                    {
                        "children": undefined,
                        "harmonicTimes": "",
                        "id": 222,
                        "label": "A相",
                        "name": "A相",
                        "parentId": 7,
                        "signalTypeIds": { "0": 1, "1": 94, "2": 161, "3": 228, "4": 295 },
                        "value": "A相"
                    }
                ]
            }
        ]
    },
    {
        "id": 3,
        "name": "电压电流功率",
        "parentId": 0,
        "signalTypeIds": "",
        "harmonicTimes": "",
        "children": [
            {
                "id": 4,
                "name": "相电压",
                "parentId": 3,
                "signalTypeIds": {
                    "0": 1,
                    "1": 94,
                    "2": 161,
                    "3": 228,
                    "4": 295
                },
                "harmonicTimes": "",
                "children": []
            }
        ]
    }
];

现在,我们可以使用以下递归函数来查询并返回id为222的对象的父节点数据:

javascript 复制代码
function findParentNodeById(id, data) {
    for (const item of data) {
        if (item.children) {
            for (const child of item.children) {
                if (child.children) {
                    // 递归搜索孙节点
                    const result = findParentNodeById(id, child.children);
                    if (result) return result; // 如果在孙节点中找到,返回结果
                } else if (child.id === id) {
                    // 找到id为222的对象,返回其父节点
                    return child;
                }
            }
        }
    }
    return null; // 如果没有找到,返回null
}

const parentNode = findParentNodeById(222, data);

console.log(parentNode); // 这将输出id为222的对象的父节点数据

这段代码定义了一个findParentNodeById函数,它接受两个参数:要搜索的id和要搜索的数组。函数内部,它遍历提供的数组及其所有子数组,查找id为指定值的对象。当找到id为222的对象时,它返回该对象的父节点数据。如果没有找到匹配的对象,函数返回null

相关推荐
前端小L7 小时前
双指针专题(三):去重的艺术——「三数之和」
javascript·算法·双指针与滑动窗口
0和1的舞者7 小时前
Spring AOP详解(一)
java·开发语言·前端·spring·aop·面向切面
web小白成长日记7 小时前
在Vue样式中使用JavaScript 变量(CSS 变量注入)
前端·javascript·css·vue.js
QT 小鲜肉7 小时前
【Linux命令大全】001.文件管理之which命令(实操篇)
linux·运维·服务器·前端·chrome·笔记
C_心欲无痕7 小时前
react - useImperativeHandle让子组件“暴露方法”给父组件调用
前端·javascript·react.js
霖鸣8 小时前
Minecraft通过kubejs进行简单魔改
javascript
JackieDYH8 小时前
HTML+CSS+JavaScript实现图像对比滑块demo
javascript·css·html
BullSmall9 小时前
支持离线配置修改及删除操作的实现方案
前端
全栈前端老曹9 小时前
【前端路由】Vue Router 嵌套路由 - 配置父子级路由、命名视图、动态路径匹配
前端·javascript·vue.js·node.js·ecmascript·vue-router·前端路由
EndingCoder10 小时前
安装和设置 TypeScript 开发环境
前端·javascript·typescript