Difference between Array(1) and [...Array(1)] [duplicate] - javascript

This question already has answers here:
Allocate new array in javascript
(3 answers)
When can an empty array be iterated over?
(4 answers)
JavaScript "new Array(n)" and "Array.prototype.map" weirdness
(14 answers)
Closed 1 year ago.
What's the difference between Array(1) and [...Array(1)]? I was sure that both are equivalent until I saw that both behave differently regarding .map:
console.log(Array(1).map(() => 1));
console.log([...Array(1)].map(() => 1));
Why do they behave differently even though both return [undefined]?
console.log(Array(1));
console.log([...Array(1)]);

Related

Strange Behaviour when Creating an Array of Arrays with new Array and Fill in Javascript [duplicate]

This question already has answers here:
Array.prototype.fill() with object passes reference and not new instance
(7 answers)
How to add same elements to javascript array n times
(4 answers)
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 3 years ago.
When I create an array of empty arrays with the following code:
const arrayOfArrays = new Array(3).fill([])
As expected, I get [[],[],[]. However let's say I change the value with:
arrayOfArrays[0][0] = 'foo'
I expect to get [['foo'],[],[]] but I end up with [['foo'],['foo'],['foo']]. Why is this the case? How can I get it so that it works as expected?

Why let a = [{}] and a.indexOf({}) -1? [duplicate]

This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
IndexOf cannot find my object. Here is the JsFiddle
let a = [{}]
console.log(a.indexOf({}))

Can Array.prototype.includes() search for an Array? [duplicate]

This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
How to compare arrays in JavaScript?
(55 answers)
Closed 4 years ago.
Consider the following code:
let a = [
["10.237.77.82", "10.237.79.255"],
["10.237.78.2", "230.0.0.1"],
["10.237.78.2", "10.237.79.255"]
]
console.log(a.includes(["10.237.77.82", "10.237.79.255"]))
I was expecting the includes() to find the Array ["10.237.77.82", "10.237.79.255"], why isn't it the case?
Array.prototype.includes() documentation does not seem to suggest specific elements which are "foundable" (although the examples are only with an Integer or a String)

Number functions and other functions issue [duplicate]

This question already has answers here:
Calling the toFixed method on a number literal [duplicate]
(3 answers)
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 5 years ago.
Can someone logicaly explain why fallowing function return error
50.toFixed(2);
and this not
(50).toFixed(2);
Same happening with other similar number functions

node-hashtable - how to get set of all keys? [duplicate]

This question already has answers here:
How to list the properties of a JavaScript object?
(18 answers)
Closed 8 years ago.
In Java it's called the keyset.
https://www.npmjs.org/package/node-hashtable
how come there is no method to return a list of all the keys?
Looking at the source there seems to be a method indexes which returna a list of the keys.

Categories

Resources