Make a summary every day, persistence is victory!
/** @date 2021-06-10 @description class array to array */Copy the code
One (sequence)
An array of classes has the length property and the key increases from 0 to 0, but there are no array methods (arguments, NodeList, etc.). Today I’ll summarize several ways to convert an array of classes into an array.
Ii (Method)
Common code:
const nodeList = document.querySelectorAll('body'); // NodeList [body]
Copy the code
- use
Array.from()
;
const arr = Array.from(nodeList); // [body]
Copy the code
- use
Extended operator
;
const arr = [...nodeList]; // [body]
Copy the code
- use
Array.prototype.concat()
;
const arr = Array.prototype.concat.call(str); // [body]
Copy the code
- use
Array.prototype.slice()
;
const arr = Array.prototype.concat.apply([], str); // [body]
Copy the code