在 JavaScript 中创建单值数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7833806/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:46:17  来源:igfitidea点击:

Create a single value array in JavaScript

javascriptarrays

提问by Imran Omar Bukhsh

why is the following code showing undefined? Are we not allowed to create an array with a single value? Putting two values won't show this error. Is this a problem with Javascript?

为什么显示以下代码undefined?我们是否不允许创建具有单个值的数组?放置两个值不会显示此错误。这是 Javascript 的问题吗?

<script>
var tech = new Array(21);
alert(tech[0]);
</script>

回答by Rob W

new Array(21)creates an array with a length of 21. If you want to create a single-value array, consisting of a number, use square brackets, [21]:

new Array(21)创建一个长度为 21 的数组。如果要创建一个由数字组成的单值数组,请使用方括号,[21]

var tech = [ 21 ];
alert(tech[0]);

If you want to dynamically fill an array, use the .pushmethod:

如果要动态填充数组,请使用以下.push方法:

var filler = [];
for(var i=0; i<5; i++){
    filler.push(i); //Example, pushing 5 integers in an array
}
//Filler is now equivalent to: [0, 1, 2, 3, 4]

When the Array constructor receives one parameter p, which is a positive number, an array will be created, consisting of pelements. This feature is can be used to repeat strings, for example:

当 Array 构造函数接收到一个参数p,它是一个正数时,将创建一个由p 个元素组成的数组。此功能可用于重复字符串,例如:

var repeat = new Array(10);
repeat = repeat.join("To repeat"); //Repeat the string 9x

回答by Samuli Hakoniemi

by new Array(21)you're actually creating an array with 21 elements in it.

通过new Array(21)您实际上是在创建一个包含 21 个元素的数组。

If you want to create an array with single value '21', then it's:

如果要创建具有单个值“21”的数组,则为:

var tech = [21];
alert(tech[0]);

回答by Imran Omar Bukhsh

guys the answer was as simple as this:

伙计们,答案就这么简单:

<script>
var tech = new Array();
tech.push(21);
alert(tech[0]);
</script>

回答by DILEEP THOMAS

You can create an Array with a value using Array.of

您可以使用Array.of创建一个带有值的数组

let arr = Array.of(8)
console.log(arr)

回答by Merianos Nikos

The above example defining an array called tech with 21 positions. You have to difine it like that

上面的例子定义了一个名为 tech 的数组,有 21 个位置。你必须像那样定义它

var tech = new Array('21');
alert(tech[0]);

回答by bhv

can use pushor use bracketsnotation , passing single value is like initialize length

可以使用push或使用括号表示法,传递单个值就像初始化长度

var tech = new Array();
tech.push(10);
console.log(tech[0]); 



var tech = new Array(5);
console.log(tech.length);  // length = 5
tech.push(10);  // will add as a 6th element i.e. a[5]
console.log(tech.length);   // length = 6
console.log(tech[0]);   // undefined 
console.log(tech[5]);  // 10 



or the easy way

或者简单的方法

var tech = [10];
console.log(tech[0]); 

回答by Edgar256

Here is my solution.

这是我的解决方案。

    var tech = new Array(); //create an empty array
    tech.push(21); //Append items to the array
    console.log(tech); // console array the array

Be careful to avoid this; var array = new Array(n), this creates an empty array of length n, then if you push; you will simply append the new item to the end of the array and the new array length will be n+1and your new array will look like this array = [Empty*21,new item ]

小心避免这种情况; var array = new Array(n),这将创建一个长度为 n 的空数组,然后如果您按下;您只需将新项目附加到数组的末尾,新的数组长度将为n+1,您的新数组将如下所示array = [Empty*21,new item ]