what's the role of slice method here ? why need it? [duplicate] - javascript

This question already has answers here:
Why do Array.prototype.slice.call(nodeList) for DOM elements?
(2 answers)
Explanation of [].slice.call in javascript?
(9 answers)
Closed 2 years ago.
In ES5 if have a NodeList and want to loop through it must first convert it to Array with slice and call. I can't understand correctly how this statement works. I need explanation.
let nodeList = document.querySelectorAll('div');
let nToArr = Array.prototype.slice.call(nodeList);
// Then Loop Through it ....

Related

Jquery Reimplementation [duplicate]

This question already has answers here:
Select all elements with a "data-xxx" attribute without using jQuery
(8 answers)
Find an element in DOM based on an attribute value
(11 answers)
How to get element by class name? [duplicate]
(4 answers)
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 12 months ago.
hey would love to have a take from a more experienced coder about this.
Would appreciate if you could give it to me in the below code format, thanks a lot
You could use document.querySelectorAll.
This returns a Node List, to convert that to an Array, you could do
const elements = [...document.querySelectorAll(".my-selector")];
That's called Vanilla Javascript
An example
var myArray = document.getElementsByClassName('myClass')
myArray will be a HTMLCollection

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?

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)

Is the dom automatically addressable in JavaScript? [duplicate]

This question already has answers here:
Why don't we just use element IDs as identifiers in JavaScript?
(5 answers)
Do DOM tree elements with IDs become global properties?
(5 answers)
Closed 5 years ago.
It seems like these two statements produce the same output.
console.log(myId)
console.log(document.getElementById('myId'))
<div id="myId">Hello World!</div>
Q: Is it safe to use myId instead of document.getElementById('myId')

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