2

Can javascript implement pass-by-reference techniques on function call? You see, I have the JSON below and I need to traverse all its node. While traversing, if the current item is an Object and contains key nodes, I must add another property isParent: true to that exact same item. But I'm having difficulty on creating a traversal function with such feature, and I tried to search for traversal functions, but all I found only returns a new JSON object instead of changing the exact JSON that is being processed.

var default_tree = [
    {
        text: "Applications",
        nodes: [
            {
                text: "Reports Data Entry",
                nodes: [
                    { text: "Other Banks Remittance Report" },
                    { text: "Statement of Payroll Deduction" },
                    ...
                ]
            },
            {
                text: "Suspense File Maintenance",
                nodes: [
                    { text: "Banks with Individual Remittances" },
                    { text: "Employers / Banks with Employers" },
                    ...
                ]
            }
        ]
    },
    {
        text: "Unposted Transactions",
        nodes: [
            { text: "Unposted Borrower Payments"},
            { text: "Unposted CMP Payments"}
        ]
    },
    { text: "Maintenance" },
    {
        text: "Reports",
        nodes: [
            {
                text: "Daily Reports",
                nodes: [
                    {
                        text: "List of Remittance Reports",
                        nodes: [
                            { text: "Banks" },
                            ...
                            {
                                text: "Employers-LBP",
                                nodes: [
                                    { text: "Employers-Zonal" }
                                ]
                            },
                        ]
                    },
                    ...
                ]
            },
            ...
        ]
    }
]

Considering we have this traversal function:

function traverse(json_object) {
    // perform traversal here
}

traverse(default_tree)

After it runs the traverse function, the default_tree's value will remain the same unless we do something like:

default_tree = traverse(default_tree)

Can someone help me create an iterator will really alter the Object being processed while iterating, instead of returning a new Object?

5
  • 1
    If obj refrences an object, then obj.foo = 42 mutates that object. It does not create a new one. However, JS is always pass-by-value, but I assume you misused the term pass-by-reference anyway. Also please note hat you an array of objects. This has nothing to do with JSON. Commented Jul 8, 2015 at 8:18
  • @Gideon there are nested object containing 'node' .You want same thing for that also.? Commented Jul 8, 2015 at 8:18
  • @RIYAJKHAN Yes, The iterator needs to traverse up to the deepest node to check and add the isParent: true key/value as necessary. Commented Jul 8, 2015 at 8:19
  • Just add the property, there is nothing special to do. Commented Jul 8, 2015 at 8:20
  • "After it runs the traverse function, the default_tree's value will remain the same" Not at all. It all depends on what you are doing inside the function. If default_tree is an object and you do json_object.foo = 42; inside the function, then you mutated the object. Here is a simplified example: function bar(baz) { baz.xyz = 42;}; var foo = {}; console.log(bar(foo)); Commented Jul 8, 2015 at 8:57

2 Answers 2

2

Please check this one

var default_tree = [....] //Array

function traverse(arrDefaultTree){
    arrDefaultTree.forEach(function(val,key){
         if(val.hasOwnProperty("nodes")){
            val.isParent = true;
            traverse(val.nodes);
         }
    }) 
}

traverse(default_tree);
console.log(default_tree);

Hope this helpful.

Sign up to request clarification or add additional context in comments.

Comments

0

With two functions, one that call then self. One find the nodes and the other one loop througgh the arrays.

traverseTree(default_tree);

function traverseTree (tree) {
  var i = 0, len = tree.length;
  for(;i < len; i++) {
    var obj = tree[i];
    findNodes(obj);
  }
}

function findNodes (obj) {
  var keys = Object.keys(obj);
  if (keys.indexOf('nodes') > -1) {
    obj.isParent = true;
    traverseTree(obj.nodes);
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.