[ES6] Converting an array-like object into an Array with Array.from()
ES6 with object from Array an INTO like
2023-09-14 09:00:54 时间
Array.from()
lets you convert an "iterable" object (AKA an array-like object) to an array. In this lesson, we go over grabbing DOM nodes and turing them into an array so that we can use methods like Array.filter()
and Array.forEach()
on them.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Array.from() example</title> </head> <body> <ul> <li class="product">15.99</li> <li class="product">7.99</li> <li class="product">32.99</li> <li class="product">24.99</li> <li class="product">5.99</li> </ul> </body> <script src="./index.js"></script> </html>
const products = Array.from(document.querySelectorAll('.product')); products .filter(product => parseFloat(product.innerHTML) < 10) .forEach(product => product.style.color = 'red');
What we got from document,querySelectorAll('.product') is 'NodeList', it is an array-like type, but cannot apply .filter, .map, .forEach to it. SO we use Array.from() method to convert is.
相关文章
- Js基础知识7-Es6新增对象Map和set数据结构
- 5-5 ES6的模块化的基本规则或特点
- [TypeScript] Using ES6 and ESNext with TypeScript
- [Node.js] Using ES6 and beyond with Node.js
- [AngularJS + Webpack] ES6 with BabelJS
- [ES6] 12. Shorthand Properties in ES6
- javascript案例40——tab导航栏(切换、添加、删除、修改)综合案例(es6面向对象、类的使用)
- ES6中Object的用法
- babel配置项目目录支持转换es6语法,引入非项目目录js后,引入Js转换无效
- JS与ES6高级编程学习笔记(一)——JavaScript核心组成
- 前端系列-ES6系列
- 【JS高级】ES6_模板字符串、let的简谈与应用_12