Skip to content

Commit 6138484

Browse files
authored
Create recursion.js
1 parent 878c7e0 commit 6138484

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/recursion.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Recursively calls itself to get the nested param ids.
3+
*
4+
* @param {Object} checkbox Checkbox.
5+
* @param {Array} allParamIds Used to build the ids array, by constantly pushing an id into it.
6+
*
7+
* @return {Array} allParamIds All param ids.
8+
*/
9+
const getAllParamIds = ( checkbox = {}, allParamIds = [] ) => {
10+
// Push the checkbox value(id) in allParamIds.
11+
allParamIds.push( checkbox.value );
12+
13+
/**
14+
* If child checkboxes exist, loop through each one of them,
15+
* and recursively call getAllParamIds() to push ids from children.
16+
*/
17+
const childCheckboxes = checkbox?.children ?? {};
18+
if ( Object.keys( childCheckboxes ).length ) {
19+
Object.keys( childCheckboxes ).forEach( ( key ) => {
20+
getAllParamIds( childCheckboxes[ key ], allParamIds );
21+
} );
22+
}
23+
24+
return allParamIds;
25+
};
26+

0 commit comments

Comments
 (0)