admin管理员组文章数量:1136616
In Javascript, why is
var myArray = new Array(3);
different from:
var otherArray = [null, null, null];
?
Obs: (myArray == otherArray)
returns 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?
Obs
[undefined, undefined, undefined]
is also not equal to myArray.
In Javascript, why is
var myArray = new Array(3);
different from:
var otherArray = [null, null, null];
?
Obs: (myArray == otherArray)
returns 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?
Obs
[undefined, undefined, undefined]
is also not equal to myArray.
Share Improve this question edited Mar 31, 2021 at 20:09 Mauricio Moraes asked Jan 8, 2015 at 13:32 Mauricio MoraesMauricio Moraes 7,3735 gold badges45 silver badges62 bronze badges 10 | Show 5 more comments6 Answers
Reset to default 179With EcmaScript 6 (ES2105), creating an array containing five nulls
is as easy as this:
const arr = new Array(5).fill(null);
MDN Reference
This var myArray = new Array(3);
will create an empty array. Hence, for this reason, myArray
and otherArray
are different arrays. Furthermore, even if they had the same values, three undefined
values, the arrays wouldn't be the same. An array is an object and the variable myArray
holds a reference to that object. Two objects with the same values aren't the same.
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 the case of var myArray = new Array(3)
even the indices aren't initialized, as correctly Paul pointed out in his comment.
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 output:
[]
The first point to note is that if you want to compare two Arrays
or any other Object
, you either have to loop over them or serialize them as comparing references will always give false
How can I get a variable like otherArray, which is an array full of 'nulls', but with whatever size I'd like?
Here is an alternative method for creating Arrays with 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]
I've did some research and it turned out that the Array(length).fill(null)
it not the best solution in terms of performance.
The best performance showed:
const nullArr = Array(length)
for (let i = 0; i < length; i++) {
nullArr[i] = null
}
Just look at this:
const Benchmark = require('benchmark')
const suite = new Benchmark.Suite
const length = 10000
suite
.add('Array#fill', function () {
Array(length).fill(null)
})
.add('Array#for', function () {
const nullArr = Array(length)
for (let i = 0; i < length; i++) {
nullArr[i] = null
}
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })
It shows the following results:
Array#fill x 44,545 ops/sec ±0.43% (91 runs sampled)
Array#for x 78,789 ops/sec ±0.35% (94 runs sampled)
Fastest is Array#for
You can also try [...new Array(76)]
to generate 76 undefineds.
console.log(
[...new Array(76)]
)
Or to fill with null
.
console.log(
[...new Array(76).fill(null)]
)
this worked for me
const bucket = [Array(9).fill(null)]
本文标签: JavascriptInitialize array with nullsStack Overflow
版权声明:本文标题:Javascript - Initialize array with nulls - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736953227a1957507.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
otherArray
is not the same array asmyArray
they can't be equal. – dfsq Commented Jan 8, 2015 at 13:35null
is not the undefined value in javascript:undefined
is. The first one is filled withundefined
. – forgivenson Commented Jan 8, 2015 at 13:36obj1 == obj2
will always befalse
,[1] == [1]; // false
– Paul S. Commented Jan 8, 2015 at 13:39