in Education by
From the server, I receive this JSON object. It represents an organigram of a company and the associated departments. I need to be able to choose a company, and with the ID of the company, I need to pass to an array of numbers the ID's of the associated departments. For that, I've created this recursive function. It works, but, skips 3 departments, which are placed within another department This is the JSON file { "cd": 1, "cd_base": 0, "nome": "EMPRESA A", "children": [ { "cd": 2, "cd_base": 1, "nome": "Departamento A", "children": [ { "cd": 4, "cd_base": 2, "nome": "Serviço A1", "children": [] }, { "cd": 15, "cd_base": 2, "nome": "Serviço A2", "children": [] } ] }, { "cd": 3, "cd_base": 1, "nome": "Departamento B", "children": [ { "cd": 7, "cd_base": 3, "nome": "Serviço B1", "children": [] } ] }, { "cd": 186, "cd_base": 1, "nome": "Departamento XX", "children": [] } ] } And this is the function in Typescript recursiveFunction(res: any): any[] { const numbers = new Array(); // to store the ID console.log('Im on ' + res.cd + ' | ' + res.nome); numbers.push(res.cd); if (res.children.length > 0) { console.log(res.cd + ' | ' + res.nome + ' has children'); res.children.forEach((row) => { numbers.push(row.cd); this.recursiveFunction(row); }); } else { console.log(res.cd + ' | ' + res.nome + ' doesn\'t have any children'); } return numbers; } And this is the return of that function to the console Im on 1 | EMPRESA A 1 | EMPRESA A has c Im on 2 | Departamento A 2 | Departamento A has children Im on 4 | Serviço A1 4 | Serviço A1 doesn't have any children Im on 15 | Serviço A2 15 | Serviço A2 doesn't have any children Im on 3 | Departamento B 3 | Departamento B has children Im on 7 | Serviço B1 7 | Serviço B1 doesn't have any children Im on 186 | Departamento XX 186 | Departamento XX doesn't have any children Then I log the numbers array and the result is 1,2,3,186 this.numbers.forEach(row => { console.log(row); }); // 1, 2, 3, 186 It adds the CD 1, 2, 3 and 186, but skips the 4, 7 and 15. All of those are a branch/node within another branch/node What am I missing? Is recursive the best way to do this? Is there a simpler way? Any help is appreciated JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
It's because you've defined a recursive function that returns a result, but you're not using that result. While the answer @aonepathan works, I would avoid using variables outside the scope of your function. Instead all you have to do, is to concatenate the result of the function with the current array: recursiveFunction(res: any): any[] { let numbers = new Array(); // to store the ID console.log('Im on ' + res.cd + ' | ' + res.nome); numbers.push(res.cd); if (res.children.length > 0) { console.log(res.cd + ' | ' + res.nome + ' has children'); res.children.forEach((row) => { numbers = numbers.concat(this.recursiveFunction(row)); }); } else { console.log(res.cd + ' | ' + res.nome + ' doesn\'t have any children'); } return numbers; } Another option is to pass the numbers array around to your function calls, and so you get rid of the return: recursiveFunction(res: any, numbers: any[]) { console.log('Im on ' + res.cd + ' | ' + res.nome); numbers.push(res.cd); if (res.children.length > 0) { console.log(res.cd + ' | ' + res.nome + ' has children'); res.children.forEach((row) => { this.recursiveFunction(row, numbers); }); } else { console.log(res.cd + ' | ' + res.nome + ' doesn\'t have any children'); } } The first time you'd call this, would be with a new Array: let result = new Array(); recursiveFunction(res, result); doSomething(result);

Related questions

0 votes
    From the server, I receive this JSON object. It represents an organigram of a company and the associated ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Check out this example code I saw: import * as React from 'react'; const Count: React.FunctionComponent ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    Which of these will happen if recursive method does not have a base case? (a) An infinite loop ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    SA=1000.00 AI=0.12 MP=100.00 def remainb(x): if x==0: return 0 else: return x=(SA+(AI/12)*SA)-MP for ... . run the loop until SA==0). Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I'm trying to capture the event of opening and closing the bootstrap accordion, but with the typescript the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    This gear was inserted from the Catalog. It relocates the player to the center of the map when it' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Sometimes, yoghurt becomes bitter and froths up. Why does this happen? Select the correct answer ... Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    I need to make a table with data with nested subtasks. I need to make this table recursive. There ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve ... pdf, mcq on JavaScript pdf, JavaScript questions and s...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I've got strings like these: | Released = {{start-date|June 14, 1972}} | Released = {{Start ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    Write C program that use both recursive and non recursive functions to perform Linear search for a Key value in a given list. Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    What is the meaning of recursive hints in Oracle?...
asked Dec 18, 2020 by JackTerrance
0 votes
    What is a Recursive Stored Procedure?...
asked Dec 11, 2020 in Technology by JackTerrance
0 votes
    What is recursive stored procedure?...
asked Nov 9, 2020 in Technology by Editorial Staff
...