Javascript - 用空值初始化数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27841362/
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
Javascript - Initialize array with nulls
提问by Mauricio Moraes
In Javascript, why is
在 Javascript 中,为什么是
> var myArray = new Array(3);
different from:
不同于:
> var otherArray = [*null*, *null*, *null*];
?
?
Obs: (myArray == otherArray)returns false.
Obs:(myArray == otherArray)返回false。
And also, how can I get a variable like otherArray, which is an array full of 'nulls`, but with whatever size i'd like?
而且,我怎样才能得到一个像 一样的变量otherArray,它是一个充满“空”的数组,但我想要什么大小?
EDIT
编辑
[undefined, undefined, undefined]
is also not equal to myArray.
也不等于 myArray。
采纳答案by Paul S.
First point to note is that if you want to compare two Arraysor any other Object, you either have to loop over them or serialize them as comparing references will always give false
首先要注意的是,如果要比较两个Arrays或任何其他Object,则必须循环它们或序列化它们,因为比较引用总是会给出false
how can I get a variable like otherArray, which is an array full of 'nulls', but with whatever size i'd like?
我怎样才能得到一个像 otherArray 这样的变量,它是一个充满“空值”的数组,但我想要什么大小?
Here is an alternative method for creating Arrayswith a default value for its items and all indices initialised:
这是创建数组的另一种方法,其项目和所有索引均已初始化:
function createArray(len, itm) {
var arr1 = [itm],
arr2 = [];
while (len > 0) {
if (len & 1) arr2 = arr2.concat(arr1);
arr1 = arr1.concat(arr1);
len >>>= 1;
}
return arr2;
}
Now,
现在,
createArray(9, null);
// [null, null, null, null, null, null, null, null, null]
回答by lardois
With EcmaScript 6 (ES2105), creating an array containing for example five nulls is as easy as this:
使用 EcmaScript 6 (ES2105),创建一个包含五个空值的数组就像这样简单:
const arr = new Array(5).fill(null);
回答by Christos
This var myArray = new Array(3);will create an empty array. Hence, for this reason myArrayand otherArrayare different arrays. Furthermore, even if they had the same values, three undefinedvalues, the arrays wouldn't be the same. An array is an object and the variable myArrayholds a reference to that object. Two objects with the same values aren't the same.
这var myArray = new Array(3);将创建一个空数组。因此,出于这个原因,myArray和otherArray是不同的数组。此外,即使它们具有相同的值,三个undefined值,数组也不会相同。数组是一个对象,变量myArray保存对该对象的引用。具有相同值的两个对象不相同。
For instance,
例如,
var a = new Object();
var b = new Object();
console.log(a===b); // outputs false.
In addition to this:
除此之外:
var customerA = { name: "firstName" };
var customerB = { name: "firstName" };
console.log(customerA===customerB); // outputs false.
Update
更新
Furthermore, in case of var myArray = new Array(3)even the indices aren't initialised, as correctly Paul pointed out to his comment.
此外,如果var myArray = new Array(3)连索引都没有初始化,正如保罗在他的评论中指出的那样。
If you try this:
如果你试试这个:
var array = [1,2,3];
console.log(Object.keys(array));
you will get as an output:
你会得到一个输出:
["1","2","3"];
While if you try this:
而如果你试试这个:
var array = new Array(3);
console.log(Object.keys(array));
you will get as an output:
你会得到一个输出:
[]

