title: Js: last value set and get
body: <blockquote>
  <p>the fastest way to select the last element from an array is by <code>array[length - 1]</code></p>
  </blockquote>
  <p>However, as I get a larger nested array, I get something like this:</p>
  <pre class="lang-js prettyprint-override"><code>let last_item = pathsry[
      pathsry.length - 1
      ][
          pathsry[
              pathsry.length - 1
          ].length - 1
      ]
  </code></pre>
  <p>It's not good man, therefore I overwrote following to get the last value.</p>
  <pre class="lang-js prettyprint-override"><code>// get item at last index
  Array.prototype.last = String.prototype.last = function(){
    return this[this.length - 1];
  }
  </code></pre>
  <p>which allows me to get</p>
  <pre class="lang-js prettyprint-override"><code>var arr = [[1, 2], [2, 3], [['a', 'b'], ['c', 'd']]];
  console.log(arr.last().last().last());
  // 'd'
  </code></pre>
  <p>However, if I try to set:</p>
  <pre class="lang-js prettyprint-override"><code>Array.prototype.last = function(){
      return this[this.length - 1];
  }
  
  var arr = [[1, 2], [2, 3], [['a', 'b'], ['c', 'd']]];
  arr.last().last().last() = 'dd';
  
  // ReferenceError: Invalid left-hand side in assignment
  </code></pre>
  <p>is there a way I can write a function that takes a number as parameter which indicates the nested slices, and an optional parameter of set value, which:</p>
  <pre class="lang-js prettyprint-override"><code>function arrayLast(arr, nes, val){
      // ...
  }
  
  var arra = [[1, 2], [2, 3], [['a', 'b'], ['c', 'd']]];
  
  console.log(arrayLast(arra, 3));
  // d
  
  arrayLast(arra, 3, 'dd');
  console.log(arra);
  // arr = [[1, 2], [2, 3], [['a', 'b'], ['c', 'dd']]]
  </code></pre>
  <p>appreciate for your help</p>