Javascript Array last()
Array.prototype.last = function(){ return this[this.length - 1]; };
Array.prototype.last = function() { return this[this.length-1]; }
Array.prototype.last = function(){ if(this.length > 0) return this[this.length-1]; return null;// w ww .j a va2 s. co m };
// Allows you to grab the very last element of an array. // Trivial but I've found it useful. // Usage: [1,4,3,5].last() // > 5 Array.prototype.last = function() { return this[this.length-1]; }//from ww w . ja v a 2 s . c om
Array.prototype.last = function() { if(this.length>0) { return this[this.length-1]; } else {//from w w w. ja va2s . c om return null; } }
Array.prototype.last = function() { return this[this.length - 1]; }; Array.prototype.toMap = function(prop) { if (prop) {//from w ww .j a v a2s.c o m return this.reduce((accum, next) => ({...accum, [next[prop]]: next}), {}); } return this.reduce((accum, next) => ({...accum, next: true}), {}); };
Array.prototype.last = function () { return this[this.length - 1]; }; Array.prototype.secondToLast = function () { return this[this.length - 2]; }; function sumFibs(num) { var sequence = [1, 1]; while (sequence.secondToLast() + sequence.last() <= num) { sequence.push(sequence.secondToLast() + sequence.last()); }/*from w w w .ja va2 s .co m*/ return sequence.filter(function (num) { return num % 2 !== 0; }).reduce(function (a, b) { return a + b; }); } console.log(sumFibs(4));
// A stack is something you can add to the top of and take off from the top. // When you want the last thing you put away to be the first thing you get is when it should be used // A queue goes by "first in first out" // When you add things on each goes behind what was added before Array.prototype.last = function() { return this[this.length - 1]; };
Array.prototype.last = function() { return this[this.length -1]; } Array.prototype.last = () => { console.log(this); // undefined due to "this" being resolved lexically return this[this.length -1]; // wont work because "this" is undefined } var a1 = [ 1,2,3 ]; console.log(a1.last()); // returns 3
Array.prototype.last = function () { return this.pop(); };
'use strict'/* ww w . j a va 2s . com*/ var fs = require("fs"); Array.prototype.last = function(){ return this[this.length-1]; } var lastLine = undefined; if(lastLine === undefined){ let myFile = fs.readFileSync(__dirname+"/let.js","utf8"); lastLine = myFile.split("\n").last(); } // console.log(lastLine); // try to declare myFile as a var and uncomment this line console.log(myFile); // I just wanted this line, but at least the rest of the file is NOT in memory!
Array.prototype.last = function last() { return this[this.length - 1]; };
Array.prototype.last = function() { return this[this[(this.length - 2)]] }
// Get the last item from an array Array.prototype.last = function() { return this[this.length - 1]; }; // Iterate over an object, like map but cooler Object.prototype.map = function(callback) { Object.keys(this).forEach(function(key, index) { if (this.hasOwnProperty(key)) { this[key] = callback(this[key], key, this); }/*ww w. ja v a 2s . c o m*/ }.bind(this)); }
if(!Array.prototype.last){ Array.prototype.last = function() { return this[this.length-1]; };//from w w w . j a v a2s. co m } if (!Array.prototype.forEach) { Array.prototype.forEach = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) fun.call(thisp, this[i], i, this); } }; } Array.prototype.clone = function() { return this.slice(0); };
Array.prototype.last = function() { return this[this.length - 1]; } Array.prototype.remove = function(from, to) { var rest = this.slice((to || from) + 1 || this.length); this.length = from < 0 ? this.length + from : from; return this.push.apply(this, rest); };