我用chatGPT生成"树对象数组中查找给定子ID的父数据"的例子

256 阅读1分钟

看到介绍GPT-4代码编辑器,果断下载尝鲜,还是蛮有趣的,离下岗更进一步😊。

第一步:条件查询

点击右上角机器人,进行条件查询:查询树对象数组的某个子id,返回对应的父数据

11.png

const tree = [
  {
    id: 1,
    name: 'Parent 1',
    children: [
      {
        id: 2,
        name: 'Child 1',
        children: [
          {
            id: 3,
            name: 'Grandchild 1',
            children: []
          },
          {
            id: 4,
            name: 'Grandchild 2',
            children: []
          }
        ]
      },
      {
        id: 5,
        name: 'Child 2',
        children: []
      }
    ]
  },
  {
    id: 6,
    name: 'Parent 2',
    children: [
      {
        id: 7,
        name: 'Child 3',
        children: []
      }
    ]
  }
];

function findNodeById(tree, nodeId) {
  let result = null;
  function traverse(currentNode) {
    if (currentNode.id === nodeId) {
      result = currentNode;
      return true;
    }
    if (currentNode.children) {
      for (let i = 0; i < currentNode.children.length; i += 1) {
        const child = currentNode.children[i];
        if (traverse(child)) {
          return true;
        }
      }
    }
    return false;
  }
  traverse(tree);
  return result;
}

function findParentNodes(tree, nodeId) {
  const parents = [];
  function traverse(currentNode) {
    if (currentNode.id === nodeId) {
      return true;
    }
    if (currentNode.children) {
      for (let i = 0; i < currentNode.children.length; i += 1) {
        const child = currentNode.children[i];
        if (traverse(child)) {
          parents.push(currentNode);
          return true;
        }
      }
    }
    return false;
  }
  traverse(tree);
  return parents;
}

const childId = 4;
const childNode = findNodeById(tree, childId);  // 生成的代码调用有问题 不应该传tree数组,应该传对象
const parentNodes = findParentNodes(tree, childId); // 生成的代码调用有问题 不应该传tree数组,应该传对象
const parentData = parentNodes.map(node => node.name); // 生成的代码调用有问题

console.log(parentData); // ['Parent 1', 'Child 1']

第二步:验证

函数的逻辑都是正确的,但是在引用函数时出现引用逻辑错误,生成的参数tree是数组,但是逻辑体内tree是对象,所以进行了以下修改

第三步:修正

const tree = [
    {
        id: 1,
        name: 'Parent 1',
        children: [
            {
                id: 2,
                name: 'Child 1',
                children: [
                    {
                        id: 3,
                        name: 'Grandchild 1',
                        children: [],
                    },
                    {
                        id: 4,
                        name: 'Grandchild 2',
                        children: [],
                    },
                ],
            },
            {
                id: 5,
                name: 'Child 2',
                children: [],
            },
        ],
    },
    {
        id: 6,
        name: 'Parent 2',
        children: [
            {
                id: 7,
                name: 'Child 3',
                children: [],
            },
        ],
    },
]

function findNodeById(treeObj, nodeId) {
    let result = null
    function traverse(currentNode) {
        if (currentNode.id === nodeId) {
            result = currentNode
            return true
        }
        if (currentNode.children) {
            for (
                let i = 0;
                i < currentNode.children.length;
                i += 1
            ) {
                const child = currentNode.children[i]

                if (traverse(child)) {
                    return true
                }
            }
        }
        return false
    }
    traverse(treeObj)
    return result
}

function findParentNodes(treeObj, nodeId) {
    const parents = []
    function traverse(currentNode) {
        if (currentNode.id === nodeId) {
            return true
        }
        if (currentNode.children) {
            for (
                let i = 0;
                i < currentNode.children.length;
                i += 1
            ) {
                const child = currentNode.children[i]
                if (traverse(child)) {
                    parents.push(currentNode)
                    return true
                }
            }
        }
        return false
    }
    traverse(treeObj)
    return parents
}

const childId = 3
//修正函数引用部分
for (let index = 0; index < tree.length; index++) {
    const item = tree[index]
    const childNode = findNodeById(item, childId)
    const parentNodes = findParentNodes(item, childId)
    const parentData = parentNodes.map((node) => node.name)
    console.log(parentData, 'parentData..') // => ['Child 1', 'Parent 1']
    if (parentData.length > 0) {
        break
    }
}

结果

虽然找问题浪费些时间,但是的确大大提高了效率,小伙伴可以试试看哦