Saturday, November 17, 2012

Getting the last element of an array with a split in Javascript

I\’m trying to get the last array item after doing a split on a string using javascript.



var str =\"Watch-The-Crap-456\".split(\'-\')[this.length];
console.log(str);​​​
// want it to console log 456, now it consoles WATCH which is in array[0]


I tried doing [this.length - 1] to get me the last array item, but it gives me undefined, I know some of you might say create another variable to store the array, but it\’s interesting to see if we can keeps things shorter.






Rating: 5 out of 5 based on 5 ratings



The post Getting the last element of an array with a split in Javascript appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/getting-the-last-element-of-an-array-with-a-split-in-javascript.html

No comments:

Post a Comment