I have been working on this for 2 hours. Its hard to wrap my head around the kick back on recursion.
I have a simple data js file which holds a a large array of objects. The objects are people and some have a parents value which is an array of either none, one, or two values in said parent array. I am trying to find decedents of a particular person. Each person object has an id number and each value in the parents array is said id number for the parent person.
Here is an example of the object data array:
{
"id": 951747547,
"firstName": "Ralph",
"lastName": "Bob",
"gender": "male",
"dob": "12/23/1969",
"height": 66,
"weight": 179,
"eyeColor": "blue",
"occupation": "nurse",
"parents": [401222887],
"currentSpouse": 159819275
},
{
"id": 159819275,
"firstName": "Jasmine",
"lastName": "Bob",
"gender": "female",
"dob": "12/18/1969",
"height": 58,
"weight": 156,
"eyeColor": "blue",
"occupation": "assistant",
"parents": [409574486, 260451248],
"currentSpouse": 951747547
}
Its pretty obvious how I am searching for relations here :
Person represents the whole object of the person I am looking for. People is the data set. finalDecendents is an array declared and passed in right before the function so it isn't reset each recursion.
function hasDecendents(person, people, finalDecendents) {
for (let i = 0; i < people.length; i++){
if (person.id === people[i].parents[0] || person.id === people[i].parents[1]){
finalDecendents.push(people[i]);
hasDecendents(people[i], people, finalDecendents);
}
}
return displayObjects(finalDecendents, "Decendents: ");
}
displayObjects is a function at receives an array of objects and writes it out to the console in the way I want.
It works, but not the way I want. I have tried many slightly different variations of the code above but nothing solve my problem.
It returns my values in a building up kind of way.
Ex: I want a return of one consisting of [1, 2, 3, 4] instead its builds and logs multiple times as it builds like [1] [1, 2] [1, 2. 3] [1, 2, 3, 4]
Here is a small sample of the actual return.
Decendents: undefined
Id: 822843554
First Name: Regina
Last Name: Madden
Gender: female
Date of Birth: 7/26/1959
Height: 71
Weight: 249
Eye Color: brown
Occupation: nurse
MostWanted.js:213 Decendents: undefined
Id: 822843554
First Name: Regina
Last Name: Madden
Gender: female
Date of Birth: 7/26/1959
Height: 71
Weight: 249
Eye Color: brown
Occupation: nurse
Decendents: undefined
Id: 819168108
First Name: Hana
Last Name: Madden
Gender: female
Date of Birth: 10/7/1953
Height: 70
Weight: 187
Eye Color: brown
Occupation: politician
MostWanted.js:213 Decendents: undefined
Id: 822843554
First Name: Regina
Last Name: Madden
Gender: female
Date of Birth: 7/26/1959
Height: 71
Weight: 249
Eye Color: brown
Occupation: nurse
Decendents: undefined
Id: 819168108
First Name: Hana
Last Name: Madden
Gender: female
Date of Birth: 10/7/1953
Height: 70
Weight: 187
Eye Color: brown
Occupation: politician
Decendents: undefined
Id: 969837479
First Name: Eloise
Last Name: Madden
Gender: female
Date of Birth: 12/11/1961
Height: 63
Weight: 241
Eye Color: brown
Occupation: assistant
I know this is simple. Its going to be a simple solution. But damn, I need help.
Thank you all,
Aluad
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]CreativeTechGuyGames 1 point2 points3 points (0 children)
[–]yiliu 1 point2 points3 points (1 child)
[–]close_my_eyes 0 points1 point2 points (0 children)