2
\$\begingroup\$

Sorry folks, but what I've got for you today is a golf but just limited by JS (or, to be strict, for a day of posting - an ES6 - the most modern set of it - if its ES7 - feel free to EDIT! use MOST MODERN features).

So if you afford that restriction - let's proceed!

Assume we have JSON root data structure r and some global ID g=0:

g=0;r={i:g++,x:0,c:[]}

This creates a root r of a data tree, indexed by unique index i, with a data payload x, where we (for simplicity) let x to be always zero, and child array c where c[j] just an object of same syntax, as r its own. Sorry for extra data x, indeed, we will don use it ever! Our goal is to do golfed ID lookup method!

My grandfather implementation was quite a mess... Just imagine, in his times people named functions with more than one symbol! Just like that:

find_subid = (id, childVec) => {
    for( child of childVec ) {
        found = find_id( id, child );
        if( found ) // 200 
            return found;
        }
        return null; // 404
    }
find_id = (id, parent) => (id == parent.i) ? parent : find_subid(id, parent.c);

Now I can call find_id(123,g) but.. I just feel that's too long to write today!

So we should fix his errors and do our own, faster, harder and shorter version in most possible modern JS as of the time you are reading this text!

\$\endgroup\$

closed as unclear what you're asking by Shaggy, fəˈnɛtɪk, Erik the Outgolfer, fergusq, Riker Feb 9 at 20:07

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    \$\begingroup\$ "json" is a specific textual way of representing javascript objects, it does not mean javascript objects themselves. \$\endgroup\$ – Sparr Feb 9 at 19:57
  • 1
    \$\begingroup\$ @Shaggy what is unclear? I am asking to golf some existing javascript code! \$\endgroup\$ – xakepp35 Feb 9 at 20:54
  • 1
    \$\begingroup\$ I believe the return null; needs to be after the for-loop, not inside. Your question could use some test cases and a clear specification how the json is allowed to look. \$\endgroup\$ – Laikoni Feb 11 at 19:32
  • \$\begingroup\$ I would consider this question as unclear as 1. You should fix up your sample code as mentioned above. 2. You should and some testcases so we can test our implementation. 3. You should include a clear description about what you want to implement, and define the "ID lookup method". \$\endgroup\$ – tsh Feb 13 at 2:32

Browse other questions tagged or ask your own question.