cadaddadadaddddaddddddr - linked list accessing Create a function (or closest equivalent, or full program) that takes an list of some datatype (your choice) that may be nested and a string (in either order), and generalizes the lisp c[ad]+r functions.

Functions are provided which perform compositions of up to four car and cdr operations. Their names consist of a C, followed by two, three, or fourany positive number occurrences of A or D, and finally an R. The series of A's and D's in each function's name is chosen to identify the series of car and cdr operations that is performed by the function. The order in which the A's and D's appear is the inverse of the order in which the corresponding operations are performed.

(source: common lisp hyperspec)
The car and cdr operations will return the first element and all the other elements, respectively, for the purposes of this challenge. The string will hold the operation you should perform.
Examples:
   Array    ,  Accessor  =&gt;  Result
===================================
     [0,1,2],      'cdr' =&gt; [1,2]
 [0,[1,2],3],     'cadr' =&gt; [1,2]
 [[[[[0]]]]],   'caaaar' =&gt; [0]
          [],      'cdr' =&gt; do not need to handle (no cdr)
          [],      'car' =&gt; do not need to handle (no car)
    anything, 'cr'/'asd' =&gt; do not need to handle (invalid command)
         [0],      'car' =&gt; 0
         [0],      'cdr' =&gt; []

Reference Implementation


{

  const inp = document.getElementById("inp");
  const out = document.getElementById("out");
  inp.onchange = () =&gt; {
    const data = JSON.parse(inp.value);
    var list = data.list;
    data.cmd.match(/^c([ad]+)r$/)[1]
      .split("").reverse().forEach(letter =&gt;
        list = letter === "a" ? list[0] : list.slice(1));
    out.value = JSON.stringify(list);
  };
  inp.onchange();

}
&lt;textarea id="inp" rows="2" cols="50"&gt;
{ "cmd": "cadadr",
  "list": [0,[1,[2,3],4],5] }
&lt;/textarea&gt;
&lt;br&gt;
&lt;textarea readonly id="out" rows="1" cols="50"&gt;
waiting, there seems to be a problem
&lt;/textarea&gt;



This is code-golf, shortest solution in bytes per language wins.
JavaScript (V8), 48 bytes

a=&gt;f=b=&gt;b[2]?b[[x,...r]=f(b.slice(1)),1]&lt;f?x:r:a

Try it online!
-2 bytes thanks to @Arnauld!