使用 Javascript 是否可以使用可变长度数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2504990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are variable length arrays possible with Javascript
提问by Ankur
I want to make a variable length array in Javascript.
我想在 Javascript 中创建一个可变长度的数组。
Is this possible. A quick google search for "Javascript variable length array" doesn't seem to yield anything, which would be surprising if it were possible to do this.
这可能吗。对“Javascript 可变长度数组”的快速谷歌搜索似乎没有产生任何结果,如果有可能做到这一点,这将是令人惊讶的。
Should I instead have a String that I keep appending to with a separator character instead, or is there a better way to get a varible length array-like variable.
我应该改为使用一个字符串来代替分隔符,还是有更好的方法来获得可变长度的类似数组的变量。
回答by SLaks
Javascript arrays are not fixed-length; you can do anything you want to at any index.
Javascript 数组不是固定长度的;你可以在任何索引处做任何你想做的事情。
In particular, you're probably looking for the pushmethod:
特别是,您可能正在寻找以下push方法:
var arr = [];
arr.push(2); //Add an element
arr.push("abc"); //Not necessarily a good idea.
arr[0] = 3; //Change an existing element
arr[2] = 100; //Add an element
arr.pop(); //Returns 100, and removes it from the array
For more information, see the documentation.
有关更多信息,请参阅文档。
回答by Spencer
Yes, variable-length arrays are possible with Javascript's Array prototype. As SLaks noted, you can use .push()and .pop()to add and remove items from the end of the array respectively and the array's length property will increase and decrease by 1 respectively each time.
是的,使用 Javascript 的 Array 原型可以实现可变长度数组。正如 SLaks 所指出的,您可以使用.push()和.pop()分别从数组的末尾添加和删除项目,并且数组的长度属性每次将分别增加和减少 1。
You can also set the value of a specific index in an array like so:
您还可以设置数组中特定索引的值,如下所示:
const arr = [];
arr[100] = 'test';
console.log(arr[100]); // 'test'
console.log(arr.length); // 101
console.log(arr[99]); // undefined
Every other array index besides index 100will be undefined.
除了 index 之外的所有其他数组索引100都是undefined.
You can also adjust the array's length by simply setting the array's length property, like so:
您还可以通过简单地设置数组的长度属性来调整数组的长度,如下所示:
const arr = [];
arr[100] = 'test';
arr.length = 1000;
console.log(arr[100]); // 'test'
console.log(arr.length); // 1000
Or...
或者...
const arr = [];
arr[100] = 'test';
console.log(arr.length); // 101
arr.length -= 10;
console.log(arr.length); // 91
console.log(arr[100]); // undefined
The maximum value that an array's length property can be is 4,294,967,295. Interestingly though, you can set values of an array at indices larger than 4,294,967,295:
数组长度属性的最大值为 4,294,967,295。有趣的是,您可以在大于 4,294,967,295 的索引处设置数组的值:
const arr1 = [];
const arr2 = [];
arr1[4294967294] = 'wow';
arr2[4294967295] = 'ok?';
console.log(arr1[4294967294]); // 'wow'
console.log(arr1.length); // 4294967295
console.log(arr2[4294967295]); // 'ok?'
console.log(arr2.length); // 0
If you try to set length a number larger than 4,294,967,295 it will throw a RangeError:
如果您尝试将长度设置为大于 4,294,967,295 的数字,它将抛出 RangeError:
const arr = [];
arr.length = 4294967296;
console.log(arr.length); // RangeError: Invalid array length
回答by rideron89
You can also use the Array()constructor.
您也可以使用Array()构造函数。
const desiredLength = 5; // could be dynamically generated
const list = new Array(desiredLength); // will be length 5
One caveat is that you will be unable to map the initial elements by using Array(n).map(). Instead, you can use Array.from()(Documentation).
一个警告是,您将无法使用 .map 来映射初始元素Array(n).map()。相反,您可以使用Array.from()(文档)。
const desiredLength = 5; // could be dynamically generated
const passkeys = Array.from(Array(desiredLength), () => {
return Math.random().toString(32).substring(2, 10);
});

