This should help!
Array.prototype.last = function(val=null) {
if (this.length === 0) {
if (val) this[0] = val;
else return null;
}
temp = this;
while(typeof temp[temp.length-1] === "object") {
temp = temp[temp.length-1];
}
if (val) temp[temp.length-1] = val; //Setter
else return temp[temp.length-1]; //Getter
}
var arr = [[1, 2], [2, 3], [['a', 'b'], ['c', 'd']]];
console.log(arr.last()); // 'd'
arr.last("dd");
console.log(arr); // [ [ 1, 2 ], [ 2, 3 ], [ [ 'a', 'b' ], [ 'c', 'dd' ] ] ]
CLICK HERE to find out more related problems solutions.