简体   繁体   English

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

[英]How to recursively iterate over a JS object, while creating a new object, with a similar structure?

I'm trying to create a "composition" of objects, where each object will have some properties, and references to other objects(let's regard them as "children"); 我正在尝试创建对象的“组成”,其中每个对象将具有一些属性,并引用其他对象(让我们将它们视为“子代”);

This composite structure is created from a config object, that lays out the structure. 此复合结构是通过对结构进行布局的配置对象创建的。 For example: 例如:

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'
                }

            ]
        }
    ]

}

I need to translate such a structure to an actual composition of object instances(I have "Link" and "Data" classes, each of which serves a different purpose: Link sends an HTTP request to that address, Data fetches a certain element). 我需要将这种结构转换为对象实例的实际组成(我有“ Link”和“ Data”类,它们各自有不同的用途:Link向该地址发送HTTP请求,Data获取特定元素)。

What i have tried so far, is a pure ridiculous hack. 到目前为止,我尝试过的只是一个荒谬的破解。 This is my recursive function: 这是我的递归函数:

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

This is the getClassMap function: 这是getClassMap函数:

 function getClassMap() {
    return {
        link: Link,
        root:Root,
        data: Data
    }

}

As you can see, i'm just modifying the original config object, adding an "actualObject" property, which holds an instance to an object, and this process repeats it self. 如您所见,我只是在修改原始配置对象,添加一个“ actualObject”属性,该属性将一个实例保存到一个对象,并且此过程会自我重复。

This is obviously totally flawed, and will only cause problems, and make my code unmaintainable. 这显然是完全有缺陷的,只会导致问题,并使我的代码无法维护。

How could i create a fresh composition of objects, according to the config "blueprint"? 根据配置“蓝图”,我如何创建对象的新组成?

The new object should look something like that, in the console: 在控制台中,新对象应如下所示:

{

    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'
                }

            ]
        }
    ]

}

Any idea would be greatly appreciated! 任何想法将不胜感激!

I attempted to make it as short as possible. 我试图使它尽可能短。 I assume you wanted to achieve something like that: 我认为您想实现这样的目标:

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

Here is a JSFiddle . 这是一个JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM