I think perhaps you are confused about how the data is structured. You are working with a nested array. Each element is either A) an element on its own or B) a sub-array of elements. You should only have two cases.
U r right, I’m only making this funtion to somehow just complete the task(not universal) and that’s bad. Now I’m tryin’ again to make it useful for each case this time . Thanks btw