Javascript 如何在创建具有类似结构的新对象时递归地迭代JS对象?

Javascript 如何在创建具有类似结构的新对象时递归地迭代JS对象?,javascript,recursion,Javascript,Recursion,我试图创建一个对象的“组合”,其中每个对象都有一些属性,以及对其他对象的引用(让我们将它们视为“子对象”) 此复合结构是从布局结构的配置对象创建的。例如: const configObj = { baseSiteUrl: `https://example.com`, startUrl: `https://example.com/products`, type:'root', children: [ { type: 'lin

我试图创建一个对象的“组合”,其中每个对象都有一些属性,以及对其他对象的引用(让我们将它们视为“子对象”)

此复合结构是从布局结构的配置对象创建的。例如:

const configObj = {

    baseSiteUrl: `https://example.com`,
    startUrl: `https://example.com/products`,
    type:'root',
    children: [
        {
            type: 'link',
            name: '.product_name_link',
            children: [
                {
                    type: 'data',
                    name: '.product_publisher'
                }

            ]
        }
    ]

}
我需要将这种结构转换为对象实例的实际组合(我有“Link”和“Data”类,每个类都有不同的用途:Link向该地址发送HTTP请求,Data获取某个元素)

到目前为止,我所尝试的是一种纯粹荒谬的黑客行为。这是我的递归函数:

function createObjectsFromTree(object) {
    const classReference = getClassMap()[object.type];//Brings a reference to the appropriate class.

    if(object.type !== 'root'){
        object.actualObject = new classReference(object.name, object.children || null);//This is the hack: it just creates a property called "actualObject", on the original object. 
    }       

    if (!object.children) {
        return;
    }

    object.children.forEach((child) => {        

        createObjectsFromTree(child)
    })

}

 createObjectsFromTree(configObj);//Making the initial call
 function getClassMap() {
    return {
        link: Link,
        root:Root,
        data: Data
    }

}
这是getClassMap函数:

function createObjectsFromTree(object) {
    const classReference = getClassMap()[object.type];//Brings a reference to the appropriate class.

    if(object.type !== 'root'){
        object.actualObject = new classReference(object.name, object.children || null);//This is the hack: it just creates a property called "actualObject", on the original object. 
    }       

    if (!object.children) {
        return;
    }

    object.children.forEach((child) => {        

        createObjectsFromTree(child)
    })

}

 createObjectsFromTree(configObj);//Making the initial call
 function getClassMap() {
    return {
        link: Link,
        root:Root,
        data: Data
    }

}
正如您所看到的,我只是修改了原始的config对象,添加了一个“actualObject”属性,它保存了一个对象的实例,这个过程会自己重复

这显然是完全有缺陷的,只会导致问题,使我的代码无法维护

如何根据配置“蓝图”创建对象的新组合

在控制台中,新对象应该是这样的:

{

    baseSiteUrl: `https://example.com`,
    startUrl: `https://example.com/products`,
    type:'root',
    children: [
        Link{// "Link" is the class name                
            name: '.product_name_link',
            children: [
                Data{// "Data" is the class name                         
                    name: '.product_publisher'
                }

            ]
        }
    ]

}

任何想法都将不胜感激

我试图使它尽可能短。 我想你想达到这样的目标:

function createObjectsFromTree(object) {
  let Class = getClassMap()[object.type];
  return new Class(
    object.name,
    (object.children || []).map(createObjectsFromTree)
  );
}

这是一个。

请将调用添加到函数中,以及所需的结果。这也会有帮助:请添加
getClassMap
以及其他缺失的函数。我已经添加了请求的项除非
返回递归调用函数的结果,否则不会发生太多的情况。torazaburo:好的,但是我该怎么做呢?我不知道。想象一下,对于每个迭代,需要在新对象上创建一个新项,以保持原始深度和索引。