简体   繁体   中英

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. 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:

 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. 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.

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.

I suggest you to check if the particular field is array, by using Array.isArray , and giving new array to the 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 object

Here is a simple recursive function to do these kind of problems.

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:

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:

getValuesByKey(test_data, key, nodes_key)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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