global.setArrayLength()
syntax: |
setArrayLength(array[, minIndex[, length]]) |
where: |
array - may be an array, buffer, or string. Though there are multiple ways to set length on these data types, setArrayLength() may be used on all three.
minIndex - the minimum index to use. Default is 0.
length - the length of the array to set.
|
return: |
void.
|
description: |
This function sets the first index and length of an array. Any elements outside the bounds set by MinIndex and length are lost, that is, become undefined. If only two arguments are passed to setArrayLength(), the second argument is length and the minimum index of the newly sized array is 0. If three arguments are passed to setArrayLength(), the second argument, which must be 0 or less, is the minimum index of the newly sized array, and the third argument is the length.
|
see: |
global.getArrayLength(), Array length, Blob.size()
|
example: |
#include <string.jsh>
var arr = [4,5,6,7]; Screen.writeln(getArrayLength(arr)); setArrayLength(arr, 5); // arr is now [4,5,6,7,,];
/******************************** The examples below illustrate using setArrayLength() with: arrays strings buffers
When appropriate alternatives exist for setting length, they are shown as comments.
These examples are not 100% exhaustive, but show most ways to use setArrayLength(). ********************************/
// Two ways to create an array // with 5 undefined elements var a1 = new Array(); setArrayLength(a1, 5); //a1.length = 5 // Does the same
var a2 = []; setArrayLength(a2, 5); //a2.length = 5 // Does the same
// Two ways to create a string // of five "\0" characters var s1 = "\0".repeat(5);
var s2 = ""; setArrayLength(s2, 5);
// Three ways to create a buffer // of five "\0" characters var b1 = new Buffer(s1);
var b2 = new Buffer(5);
var b3 = new Buffer(5); setArrayLength(b3, 5); //Blob.size(b3, 5); // Does the same //b3.length = 5; // Does the same |