简体   繁体   English

Javascript/jQuery - 对于所有多维键的每个数组迭代

[英]Javascript/jQuery - For each array iteration trough all multidimensional keys

I have a multidimensional array with the following structure:我有一个具有以下结构的多维数组:

[13] => Array
    (
        [id] => 51
        [text] => Corporate
        [seltext] => Corporate
        [parent_id] => 0
        [object_type] => folder
        [corefilename] => 
        [userUNIQUE] => 
        [nodes] => Array
            (
                [0] => Array
                    (
                        [id] => 50
                        [text] =>  Due diligence
                        [seltext] => Due diligence
                        [parent_id] => 51
                        [object_type] => folder
                        [corefilename] => 
                        [userUNIQUE] => 
                    )

                [1] => Array
                    (
                        [id] => 2
                        [text] =>  Drafts
                        [seltext] => Drafts
                        [parent_id] => 51
                        [object_type] => folder
                        [corefilename] => 
                        [userUNIQUE] => 
                        [nodes] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 6
                                        [text] =>  de
                                        [seltext] => Decisions                                                                  [parent_id] => 2
                                        [object_type] => folder
                                        [corefilename] => 
                                        [userUNIQUE] => 
                                        [nodes] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [id] => 44
                                                        [text] =>  
                                                        [parent_id] => 6
                                                        [object_type] => file
                                                        [corefilename] => f-8TYO89KHTK1BNV4QMMAW6GHEEDRREECP1VDV4Y6VLXWM7XS97V4LCUWXJFM6E61VXF
                                                        [userUNIQUE] => 

I would like to get all "id" values that are populated all the way down the array - nodes.我想获取一直填充到数组中的所有“id”值 - 节点。 Ive tried to iterate, but the only level that I can reach is the first one.我试过迭代,但我唯一能达到的水平是第一个。

This is the function that im trying to run with no success:这是我试图运行但没有成功的 function:

 var childs=$('#tree').treeview('getSelected', loc);

             function getValuesByKey(object, key) {
         var values = [];
         recursiveFx(object);
         function recursiveFx(object) {
           for (var property in object) {
             if (object.hasOwnProperty(property)) {
               if (typeof object[property] == "object") {
                recursiveFx(object[property]);
               } else {
                 try {
                   if (isDefined(object[key])) {
                     console.log('value:',object[key]);
                     values.push(object[key]);
                   }
                 } catch (e) {

                 }
               }
             }
           }
         }
         return values;
       }
getValuesByKey(childs, 'id');

What am I doing wrong?我究竟做错了什么?

As far as I see from your data, you are iterating through the array in the first place, not through an object.据我从您的数据中看到,您首先遍历数组,而不是通过 object。 The same solution can be used for an object as well, however, there has to be iteration through properties and array elements, unless you know, that some particular property will always contain an array through all the objects. object 也可以使用相同的解决方案,但是,必须对属性和数组元素进行迭代,除非您知道某些特定属性将始终包含所有对象的数组。

This is just a sample data, to save time:这只是一个示例数据,以节省时间:

var data = [
    {"id": 50},
    {
        "id": 35,
        "nodes": [
            {"id": 3},
            {"id": 7},
            {
                "id": 9,
                "nodes": [
                    {"id": 0},
                    {"id": 1}
                ]
            },
        ]
    },
];

The next thing is you take property that is in the object, and check if it has that property, which will fall true all the time.接下来是您获取 object 中的属性,并检查它是否具有该属性,这将始终为真。

I suggest you to check if the particular field is array, by using Array.isArray , and giving new array to the recursiveFx.我建议您使用Array.isArray检查特定字段是否为数组,并将新数组提供给 recursiveFx。

function getValuesByKey(arr, key) {
         var values = [];
         recursiveFx(arr);

         function recursiveFx(arr) {
           for (let el in arr) {
               object = arr[el];
               for(let property in object){
                     if(property === key){
                       values.push(object[property]);
                     } else if(Array.isArray(object[property])){
                       recursiveFx(object[property])
                     }
                }
           }

         }
         return values;
}

console.log(getValuesByKey(data, 'id'));

I think your data is an "Array"?我认为您的数据是“数组”? but used var property in objectvar property in object

Here is a simple recursive function to do these kind of problems.这是一个简单的递归 function 来解决这类问题。

let test_data = 
[{
        "id": 1,
        "somekey": "someval1",
        "nodes": [{
            "id": 2,
            "somekey": "someval2",
            "nodes": [{
                "id": 4,
                "somekey": "someval4"
            }, {
                "id": 5,
                "somekey": "someval5",
                "nodes": [{
                    "id": 6,
                    "somekey": "someval6"
                }]
            }]
        }]
    },
    {
        "id": 3,
        "somekey": "someval3"
    }
]

And recursive function:和递归 function:

const nodes_key = "nodes";
const key = "id";
let values = [];

function getValuesByKey(ary, key, nodes_key){
    for ( object of ary ){
        if (object.hasOwnProperty(key)) {
            values.push(object[key]) ;
        }
        if ( object.hasOwnProperty(nodes_key) ){
            getValuesByKey(object[nodes_key], key, nodes_key)
        } // if it has child-nodes, just do same as above.. (pushes the value of key)
    } // retrive each object in an Array
}

Output: Output:

getValuesByKey(test_data, key, nodes_key) getValuesByKey(test_data, key, nodes_key)

> values
[ 1, 2, 4, 5, 6, 3 ]

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

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