PPB
PPB

Reputation: 287

how to return when for/in function finishes in node js recursive function

I am iterating a nested json in node.js like below and want to fetch all the attrList where type is abc and create another json.

I am able to extract the required data and create output json.

But how should I return the final output array? I'm not able to figure out exiting/return condition. I am a newbie to node js, still learning. Can anyone help here?

 function recursion(input, output) {
    if (input["Type"] == "abc") {
        let attrlist = {};
          for (let i = 0; i < input["atrrlist"].length; i++) {

            attrlist[input["atrrlist"][i]["name"]] = input["atrrlist"][i]["val"];
        }
        if (input["atrrlist"].length > 0) {
            output[input["a"]] = attrlist;
        }
      }
    for (let obj in input) {
        if (typeof input[obj] == "object" && input[obj] !== null) {
             recursion(input[obj], output);
        }
    }
}

I am calling it like.

let output={};
recursion(input, output)

Input json is like below:

 {
  "a": "val",
 "b": "val2",
 "Type": "abc",
  atrrlist": [{
    "name": "vbv",
    "val": "vbv"
}],
"child": [{
    "a": "val",
    "b2": "val2",
    "Type": "abc",
    "atrrlist": [{
        "name": "vbv",
        "val": "vbv"
    }],
    "child": [{
        "a": "val",
        "b2": "val2",
        "Type": "abc",
       "atrrlist": [{
            "name": "vbv",
            "val": "vbv"
        }],
        "child": [{
            "a": "val",
            "b2": "val2",
            "Type": "xyz",
            "atrrlist": [{
                "name": "vbv",
                "val": "vbv"
            }]

        }]

    }]
}]

 }

Upvotes: 0

Views: 427

Answers (1)

trincot
trincot

Reputation: 350202

You did not specify the output structure that you expect, except that it is an array.

I would advise not to pass output as an argument, but to make it the return value of the recursion function. The main issue in your code is that you do not concatenate the recursive result to the "current" result. The only time you define output it is not an array while you write "how should I return the final output array?". So define it as an array and push the results unto it, also the recursive ones.

function recursion(input) {
    const output = [];
    if (input.Type === "abc") {
        const attrlist = {};
        for (const {name, val} of input.atrrlist) {
            attrlist[name] = val;
        }
        if (input.atrrlist.length > 0) {
            output.push({ [input.a]: attrlist });
        }
    }
    for (const obj of Object.values(input)) {
        if (Object(obj) === obj) {
            output.push(...recursion(obj));
        }
    }
    return output;
}

// Sample input:
const input = {"a": "val","b": "val2","Type": "abc","atrrlist": [{"name": "category","val": "furniture"}],"child": [{"a": "val","b2": "val2","Type": "abc","atrrlist": [{"name": "product","val": "chair"}],"child": [{"a": "val","b2": "val2","Type": "abc","atrrlist": [{"name": "color","val": "blue"}],"child": [{"a": "val","b2": "val2","Type": "xyz","atrrlist": [{"name": "vbv","val": "vbv"}]}]}]}]};

console.log(recursion(input));

Upvotes: 1

Related Questions