简体   繁体   English

jQuery嵌套Array..get直接父ID

[英]jQuery Nested Array..get immediate parent id

I have the following array 我有以下数组

array = [
{
    "id": "67",
    "sub": [
        {
            "id": "663",
        },
        {
            "id": "435",
        }
    ]
},
{
    "id": "546",
    "sub": [
        {
            "id": "23",
            "sub": [
             {
                 "id": "4",
             }
         ]
        },
        {
            "id": "71"
        }
    ]
}
]

I am currently looping throught the array as follows 我目前正在遍历数组如下

calling the array: 调用数组:

processArray(array);

the function loop 功能循环

function processArray(arr)
{
    for(var item in arr) {
        var value = arr[item];      
        var order = item;
        var itemID = value.id;

        if(itemID != null)
        {
            $('.text').append(" ORDER : " + order + " Item ID : " + itemID  + "<br />" );
        }
        if(typeof(value) == 'object') { //If it is an array,            
            processArray(arr[item]);
        }
    }   
}

Currently i am getting the order of the item and the current ID no problem. 目前,我正在获取该项目的订单和当前ID没问题。 What i need however (for my database schema) is for each item get the ID of its parent if there is one. 但是,我需要的(对于我的数据库模式)是为每个项目(如果有的话)获取其父项的ID。 Do i need to pass the parent to each node? 我是否需要将父级传递给每个节点? Or is there an easier way? 还是有更简单的方法?

Thanks 谢谢

Working demo 工作演示

Include an optional parameter parentID in the function; 在函数中包含可选参数parentID by doing this, you can still use the processArray(array); 通过这样做,您仍然可以使用processArray(array); syntax to process the original array. 处理原始数组的语法。

function processArray(arr, parentID)
{
    for(var item in arr) {
        var value = arr[item];      
        var order = item;
        var itemID = value.id;

        if(itemID != null)
        {
            var output = " ORDER : " + order + " Item ID : " + itemID;

            if( parentID ) { output += " PARENT : " + parentID; }

            $('.text').append( output + "<br />");
        }

        // PROCESS SUB-ARRAY
        if( typeof(value.sub) == 'object') { //If it is an array,            
            processArray( value.sub, itemID );
        }
    }   
}

Use an auxiliary function that has id as part of its signature: 使用具有id作为其签名的一部分的辅助函数:

function processArray(arr) {

    function _processArray(arr, id) {
        for (var item in arr) {

            var value = arr[item];
            var order = item;
            var itemID = value.id; // you need to change this because on the second call you pass in a string and not just an object
            var parentId = id;

            // Commenting the if statement that you had here actually shows the parent id's now.
            $('.text').append(" ORDER : " + order + " Item ID : " + itemID + " Parent Id : " + parentId + "<br />");


            if (typeof value === "object") { //use instanceof,            
                _processArray(arr[item], itemID);
            }
        }
    }

    _processArray(arr, 0);
}

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

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