0

As a mostly self-taught programmer, when I first learned Perl subroutines (like functions in other languages), it was common to write this kind of statement:

sub someFunction(@args) {
   (el_1, el2, el3) = @args;
}

I had been doing this procedure for years to essentially explode an array of function arguments into named variables. But, I don't remember it having a name. I just found out yesterday it was called "destructuring". That is a technical, search-friendly term that will get many results when you are researching coding principles.

Another example of a technical, search-friendly term is "nullish coalescing operator". I have no idea what that even is, but it seems like a good example of something a self-taught programmer without a bachelors degree might not automatically know, unless specifically taught it.

In much the same way, I am wanting to know what the technical, search-friendly term for recursively processing or "spidering" a JavaScript object with unknown/variable keys, that has objects or arrays nested on many levels?

WHAT I'VE TRIED:

So far searches like these have been minimally fruitful:

how to parse nested javascript object?

ES6 get properties of json object

spider nested javascript objects with variable unknown keys

One Stack Overflow post that I found promising was this one:

How can I access and process nested objects, arrays or JSON?

Near the very end of user @FelixKling's excellent answer, he describes what I'm talking about, without giving any kind of terminology for it, as thus:

A more generic way to access a nested data structure with unknown keys and depth is to test the type of the value and act accordingly.

Here is an example which adds all primitive values inside a nested data structure into an array (assuming it does not contain any functions). If we encounter an object (or array) we simply call toArray again on that value (recursive call).

function toArray(obj) {
    const result = [];
    for (const prop in obj) {
        const value = obj[prop];
        if (typeof value === 'object') {
            result.push(toArray(value)); // <- recursive call
        }
        else {
            result.push(value);
        }
    }
    return result;
}
  • 2
    It is just recursion. I do not think it has a special name. Maybe Recursive Find? – epascarello Dec 09 '21 at 14:03
  • 1
    "*it seems like a good example of something a self-taught programmer without a bachelors degree might not automatically know, unless specifically taught it.*" hi, university-taught programmer here with a master's degree. I was never taught what "nullish coalescing" was. I encountered it in the wild and that's how I learned about it. – VLAZ Dec 09 '21 at 14:04
  • 2
    At any rate, I don't think there is a special name for that technique. As epascarello says, it's "recursion" but that's just what is used to get the effect. You could apply many names to it "recursively find keys" or "recursively visit values" but these still just describe the operation with different terms. A "depth-first search" might give more relevant results, perhaps - it's the act of searching through structures similar to plain objects. DFS can easily be done via recursion. There is also "breadth first search" which is rarely done with recursion but an alternative to DFS. – VLAZ Dec 09 '21 at 14:07
  • 2
    You *might* also encounter relevant results using the term "walk" which in programming is another generic term for "going over stuff". So, "recursively walk object values" for example would mean the what that code does. – VLAZ Dec 09 '21 at 14:09
  • This question feels like it is either about genetic programming or English language – Parzh from Ukraine Dec 09 '21 at 16:41
  • 1
    I'd call it a "tree walk", but I don't know of a Universal Nomenclature. There is such a thing as "tree transduction", but it's one of those specialised academic terms which cannot be relied upon to satisfy your intuitions about details. (IOW, it's an interesting subject but only vaguely related to what you're doing.) Data structures like JSON, XML and PDF are often called "dendritic" ("having a branched form resembling a tree") and libraries to deal with them often have mechanisms to do different "traverses" (ways of navigating the dendrites.) – rici Dec 09 '21 at 17:23

0 Answers0