This question already has answers here:
What is empty inside an array? e.g. [empty X 5]
(2 answers)
Undefined values in Array(len) initializer
(5 answers)
JavaScript "new Array(n)" and "Array.prototype.map" weirdness
(14 answers)
Closed 2 years ago.
What is the difference between the undefined elements in JavaScript arrays:
created directly,
created with elision?
Elements of case 1. and 2. both have undefined types and values, but the ones born in elision don't have indexes (or any enumerable properties, their array has a correct Array.length that's clear). Can you give some background on this?
Example:
// case 1.
const undefineds = [undefined, undefined, undefined].map((el, i) => i)
console.log(undefineds, undefineds.length, 'undefineds')
// case 2.
const empties = [,,,].map((el, i) => i)
console.log(empties, empties.length, 'empties')
Output:
Array [0, 1, 2] 3 "undefineds"
Array [undefined, undefined, undefined] 3 "empties"
Note: In case 2. I am aware of not just the indexes i would be undefined, but anything returned from the iteration, e.g.: [,,,].map((el, i) => 'puppies') => [undefined, undefined, undefined].
Related
This question already has answers here:
Why doesn't splicing an object from an array in Javascript return the array?
(4 answers)
How to Clone an Array With a Removed Element? [duplicate]
(2 answers)
Closed 21 days ago.
Why does the new array only contain the value '394', instead of all the other ones?
const values = [5, 11, 394, 2, 576];
function pureSplice(givenArray: number[]){
const newArray: number[] = givenArray.splice(2, 1).map(x => x);
return newArray;
}
pureSplice(values);
The 'newArray' only contains the value '394', why does it do that and is there a way to get the array to have all values other than 394' with .splice and .map?
This question already has answers here:
Using Array.map with new Array constructor
(5 answers)
Closed 3 years ago.
If I write this code: Array(3).map( () => ({ a:1 }) ) I get Array(3) [ <3 empty slots> ] instead of an array of 3 objects. Why is that?
As far as I know, Array(3) will produce an array of undefined elements with length 3. And for instance, [1, 2, 3].map( () => ({ a:1 }) ) produces the expected output. This is true also using any other array with length 3. I'm intrigued.
Array(3) creates an empty array of length 3. Or as an object it would be { length: 3 }. With, e.g. Array(Array(3)) you would create an array with undefineds { 0: undefined, 1: undefined, 2: undefined, length: 3 }. And .map only iterates over existing keys.
This question already has answers here:
JavaScript "new Array(n)" and "Array.prototype.map" weirdness
(14 answers)
Closed 5 years ago.
consider I declare two variables like this (done within REPL, with node v7.7.2), which I expect to be arrays:
var x = Array(4)
var y = Array.from({length: 4})
then the following should work identically, but it doesn't:
x.map(Math.random)
[ , , , ]
y.map(Math.random)
[ 0.46597917021676816,
0.3348459056304458,
0.2913995519428412,
0.8683430009997699 ]
in looking, it seems x and y are both identical:
> typeof x
'object'
> typeof y
'object'
> Array.isArray(x)
true
> Array.isArray(y)
true
> x.length
4
> y.length
4
> typeof x[0]
'undefined'
> typeof y[0]
'undefined'
so why the difference?
Actually, it should not have the same results fot both cases. See the manual here about Array:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
If the only argument passed to the Array constructor is an integer
between 0 and 232-1 (inclusive), this returns a new JavaScript array
with its length property set to that number (Note: this implies an
array of arrayLength empty slots, not slots with actual undefined
values). If the argument is any other number, a RangeError exception
is thrown.
And here about the Array.from() method
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from
Check the following example on the page linked above:
// Generate a sequence of numbers
// Since the array is initialized with `undefined` on each position,
// the value of `v` below will be `undefined`
Array.from({length: 5}, (v, i) => i);
// [0, 1, 2, 3, 4]
For the first three outputs, Array#map works.
It is not called for missing elements of the array (that is, indexes that have never been set, which have been deleted or which have never been assigned a value).
The ECMA 262 standard to Array.from describes a construction with length for a new array (point 7 ff).
var x = Array(4),
y = Array.from({ length: 4 }),
arrayX = x.map(Math.random),
arrayY = y.map(Math.random),
z = Array.from({ length: 4 }, Math.random);
console.log(x);
console.log(y);
console.log(arrayX);
console.log(arrayY);
console.log(z);
The Array created with Array(4) is not iterated over with .map(), whereas the Array.from({ length: 4 }) is iterated over. A fuller explanation can be found at JavaScript "new Array(n)" and "Array.prototype.map" weirdness, you can test this with..
x.map((f,i) => console.log(i)) vs. y.map((f,i) => console.log(i))
This question already has answers here:
Undefined values in Array(len) initializer
(5 answers)
Closed 5 years ago.
Suppose we have 3 arrays:
var arr1 = [undefined, undefined, undefined];
var arr2 = [, , ,];
var arr3 = new Array(3);
Are all of them identical in JavaScript?
If we wants to use undefined as default value for fixed size array, can we use them interchangeably?
The second two are the same (though the second one might cause issues on some browsers), but the first is different.
The first one
var arr1 = [undefined, undefined, undefined];
makes explicit assignments to the first three indexes in the array. Even though you're assigning undefined, the fact that indexes 0, 1, and 2 are targets of assignment operations means that those elements of the array will be treated as "real" by methods like .reduce():
[undefined, undefined, undefined].reduce(function(c) { return c + 1; }, 0);
// 3
[,,].reduce(function(c) { return c + 1; }, 0);
// 0
Many of the Array.prototype methods skip uninitialized array elements.
This question already has answers here:
(new Array(10)).map(function() { return 1;}) returns [, , , , , ...] ... Why? [duplicate]
(3 answers)
Closed 7 years ago.
Trying to find a way to inline the creation of an array of data given its size, i tried
var n = 4;
var data = (new Array(n)).map(function(x) { return 1 });
which gave me
[undefined, undefined, undefined, undefined]
Regarding that
new Array(n)
gives
[undefined, undefined, undefined, undefined]
and that
[undefined, undefined, undefined, undefined].map(function(x) { return 1 });
returns
[1, 1, 1, 1]
I would expect to be able to chain the Array constructor and the map function.
Any insights on that behavior ?
new Array(n) creates an array of size n, but with no elements in it. This is called a sparse array. Though there are no elements in the array, the browser represents them as undefined. And Array.prototype.map will ignore the array elements which are not defined yet.
Quoting from the ECMA 5.1 Specification for Array.prototype.map,
callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.
To be able to chain the map function, you might want to do like this
console.log(Array.apply(null, {length: n}).map(function(x) { return 1 }));
# [ 1, 1, 1, 1 ]