Friday, October 12, 2012

JavaScript slice

This suggests that Orange and Lemon may be collected alone in an array together with this JavaScript:

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3);

 
 

Anthony Dominguez mentioned yesterday that if you want to make a new array from an array that you should port from one to the other with an all encompassing slice. If you do not do so, then the two arrays will reference the same information!

Addendum 7/4/2015: .slice may be called with just one parameter in which case everything before the point specified gets dropped. For example:

var citrus = fruits.slice(1);

 
 

...will give us the Orange, the Lemon, the Apple, and the Mango too dropping only the Banana. If .slice is called with more than two parameters the extra values seem to be ignored and if it is called with zero parameters it will just return everything unsliced.

No comments:

Post a Comment