当前位置: 首页 > 面试题库 >

动态嵌套对象数组的递归迭代

虞华翰
2023-03-14
问题内容

我正在使用有角JS及其示例之一:http :
//jsfiddle.net/furf/EJGHX/

在发送更新功能之前,我需要获取数据并向其中添加一些值。(如果用angular而不是js更好,请告诉我)

我正在尝试获取“父代”和“索引”并更新子代。

这是我正在遍历的数据

{
    "children": [{
        "id": "5",
        "parentid": "0",
        "text": "Device Guides",
        "index": "1",
        "children": [{
            "id": "10",
            "index": "0",
            "text": "Grandstream GXP-21XX"
        }, {
            "id": "11",
            "index": "1",
            "text": "Polycom Soundstation/Soundpoint"
        }, {
            "id": "23",
            "index": "2",
            "text": "New Polycom"
        }]
    }, {
        "id": "6",
        "parentid": "0",
        "text": "Pre-Sales Evaluation",
        "index": "0",
        "children": []
    }, {
        "id": "7",
        "parentid": "0",
        "text": "Router Setup Guides",
        "index": "2",
        "children": [{
            "id": "9",
            "index": "0",
            "text": "Sonicwall"
        }, {
            "id": "12",
            "index": "1",
            "text": "Cisco"
        }]
    }, {
        "id": "9",
        "parentid": "7",
        "text": "Sonicwall",
        "index": "0",
        "children": []
    }, {
        "id": "10",
        "parentid": "5",
        "text": "Grandstream GXP-21XX",
        "index": "0",
        "children": []
    }, {
        "id": "11",
        "parentid": "5",
        "text": "Polycom Soundstation/Soundpoint",
        "index": "1",
        "children": []
    }, {
        "id": "12",
        "parentid": "7",
        "text": "Cisco",
        "index": "1",
        "children": []
    }, {
        "id": "15",
        "parentid": "0",
        "text": "Post-Sales Implementation Check List",
        "index": "7",
        "children": [{
            "id": "16",
            "index": "0",
            "text": "Porting and New Number Details"
        }, {
            "id": "18",
            "index": "1",
            "text": "Partner Setup"
        }, {
            "id": "19",
            "index": "2",
            "text": "test"
        }, {
            "id": "21",
            "index": "3",
            "text": "test"
        }]
    }, {
        "id": "16",
        "parentid": "15",
        "text": "Porting and New Number Details",
        "index": "0",
        "children": []
    }, {
        "id": "18",
        "parentid": "15",
        "text": "Partner Setup",
        "index": "1",
        "children": []
    }, {
        "id": "19",
        "parentid": "15",
        "text": "test",
        "index": "2",
        "children": []
    }, {
        "id": "20",
        "parentid": "0",
        "text": "test",
        "index": "11",
        "children": []
    }, {
        "id": "21",
        "parentid": "15",
        "text": "test",
        "index": "3",
        "children": []
    }, {
        "id": "23",
        "parentid": "5",
        "text": "New Polycom",
        "index": "2",
        "children": []
    }, {
        "id": "24",
        "parentid": "0",
        "text": "Test Markup",
        "index": "14",
        "children": []
    }, {
        "id": "25",
        "parentid": "0",
        "text": "test",
        "index": "15",
        "children": []
    }]
}

这就是我目前正在遍历的方式,但是它只能得到第一个维度

for (i = 0, l = data.length; i < l; i++) {
    parentid = data[i].id == null ? '0' : data[i].id;
    data[i].index = i;
    if (data[i].children) {
        if (data[i].children.length > 0) {
            for (q = 0, r = data[i].children.length; q < r; q++) {
                data[i].children[q].parentid = parentid;
                data[i].children[q].index = q;
            }
        }
    }
}

我在另一个小提琴上找到了这个,但我不知道该如何抓取父对象或索引

$.each(target.children, function(key, val) { recursiveFunction(key, val) });

    function recursiveFunction(key, val) {
        actualFunction(key, val);
        var value = val['children'];
        if (value instanceof Object) {
            $.each(value, function(key, val) {
                recursiveFunction(key, val)
            });
        }

    }


function actualFunction(key, val) {}

问题答案:

如果我对您的理解正确,那么您希望每个“子代”都有一个parentID(由其父代定义0否则)和一个index(根据其在同级集中的位置)。

function normalize(parent) {
    if (parent && parent.children) {
        for (var i = 0, l = parent.children.length; i < l; ++i) {
            var child = parent.children[i];
            child.index = i;
            if (!child.parentId) child.parentId = parent.id || '0';
            normalize(child);
        }
    }
}

normalize(data);


 类似资料:
  • 问题内容: 前几天,我以为我在jQuery中看到了一个对象迭代器,该对象迭代器具有可以设置为对子对象进行递归迭代的标志。我以为它是jQuery.each()的一部分,但是现在我在文档中看不到该功能。 jQuery中是否有任何此类迭代器可以自动递归? (我知道如何用JavaScript进行操作。只是想知道我是否确实看到了我以为看到的东西。) 非常感谢! 编辑: 要清楚,我在想像jQuery.each

  • mapMap.map(i => i * 30); const mapReduce = Immutable.Map({ a: 10, b: 20, c: 30 });

  • 问题内容: 我有一个具有与组件本身相同类型的子组件的组件。我也可以渲染孩子,但是孩子似乎无法访问他们的状态。我在Redux中使用React 另外,我正在使用Material-ui中的ListItem(它们最终呈现在List组件中),并且我的代码受到官方redux回购中的树视图示例的严重影响。我的状态如下所示: 所示状态与树状示例中的状态相同 问题答案: 好吧,如果我理解正确,那么您希望这些组件从r

  • 我想在一个带有递归的Javascript嵌套对象中找到一个值的键。 下面是我对函数的尝试。有没有更优雅的方法来实现这一点? null null 例如,有没有一种方法可以避免检查的值然后中断?我觉得result应该能够在调用堆栈中冒泡,直到它在第一次函数调用时返回,而不必中断。

  • 问题内容: 我有一个对象,它可以是任何数量的深度,并且可以具有任何现有属性。例如: 在此我想设置(或覆盖)属性,如下所示: 属性字符串可以具有任何深度,并且值可以是任何类型/事物。 如果属性键已经存在,则不需要合并对象和数组作为值。 前面的示例将产生以下对象: 如何实现这种功能? 问题答案: 此函数使用您指定的参数应添加/更新容器中的数据。请注意,您需要跟踪架构中的哪些元素是容器,哪些是值(字符串

  • 我有一个对象,可以是任何数量的层次深,可以有任何现有的属性。例如: 在此基础上,我希望设置(或覆盖)如下属性: 属性字符串可以有任何深度,值可以是任何类型/事物。 如果属性键已经存在,则不需要合并作为值的对象和数组。 上一个示例将生成以下对象: 如何实现这样的功能?