Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created May 10, 2023 18:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/58bbd1d0976e93d779437bfcd6174de5 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/58bbd1d0976e93d779437bfcd6174de5 to your computer and use it in GitHub Desktop.
Code from video about practical uses for Array.from() and all of it's parameters applied to Objects and NodeLists and copied Arrays
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="main.js" type="module"></script>
</head>
<body>
<h1>Array.from() STATIC method</h1>
<main>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Repudiandae doloremque, mollitia nemo ratione cupiditate vel recusandae et vitae animi voluptatibus soluta ipsam nesciunt fuga facere
odio enim perferendis sint placeat?
</p>
<p>
Maxime, quibusdam. Odio at voluptatem perspiciatis mollitia. Accusantium doloribus ipsa eveniet ratione? Animi eius iste voluptas qui ipsum illo esse at reprehenderit consequatur veniam earum
atque ullam maxime, totam repudiandae?
</p>
</main>
</body>
</html>
const log = console.log;
let paras = document.querySelectorAll('p');
let obj = {
id: crypto.randomUUID(),
name: 'Sterling Archer',
email: 'sterling@isis.org',
};
// log(typeof paras);
// log(paras.constructor); //NodeList
let a1 = Array.from(paras, (p) => p.localName);
// log(a1);
let a2 = Array.from(obj);
// log(a2); // [ ]
obj[Symbol.iterator] = function () {
let iterableProps = ['id', 'name', 'email'];
let index = 0;
return {
next() {
let value = obj[iterableProps[index]];
index++;
return { value, done: index === iterableProps.length + 1 };
},
};
};
a2 = Array.from(obj);
// log(a2); // [ ]
a2 = Array.from(obj, (val) => (val.includes('@') ? null : val));
// log(a2);
let a3 = Array.from(
obj,
function (item, index) {
if (this[index]) {
return { [item]: this[index].innerText.split(' ').shift() };
} else {
return item;
}
},
paras
);
log(a3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment