Array.prototype.at

[ad_1]

Working with arrays is a vital talent in any programming language, particularly JavaScript, as we proceed to depend on exterior information APIs. JavaScript has added strategies like discover and `findIndex lately, however one syntax I like from languages like Python is retrieving values by unfavourable indexes.

While you need to get the worth of the final merchandise in an array, you find yourself with an archaic expression:

const arr = ["zero", "one", "two", "three"];
const final = arr[arr.length - 1];

You would use pop however that modifies the array. As a substitute you should utilize at and an index, even a unfavourable index, to retrieve values:

const arr = ["zero", "one", "two", "three"];
arr.at(-1); // "three"
arr.at(-2); // "two"
arr.at(0); // "zero"

at is a little or no recognized perform however helpful, if just for the shorthand syntax!

  • DWRequest: MooTools 1.2 AJAX Listener & Message Display

    Although MooTools 1.2 is in its second beta stage, its primary syntax and principle modifications have been hashed out. The JavaScript library continues to enhance and grow to be extra versatile. Fellow DZone Zone Chief Boyan Kostadinov wrote a really helpful article detailing how one can add a…

  • Create a Spinning, Zooming Effect with CSS3

    In case you were not conscious, CSS animations are superior.  They’re clean, much less taxing than JavaScript, and are the way forward for node animation inside browsers.  Dojo’s cellular answer, dojox.cellular, makes use of CSS animations as a substitute of JavaScript to lighten the appliance’s JavaScript footprint.  Certainly one of my favourite results…


[ad_2]

Leave a Reply