[ES6] Converting an array-like object into an Array with Array.from()
2015-11-22 23:04
573 查看
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.
相关文章推荐
- javaScript与Objective-C交互
- 增强现实(AR)笔记4:ObjectRecognition实操
- Java中的Object类
- Understanding Runtime
- Objective-C Runtime 运行时之五:协议与分类
- Objective-C Runtime 运行时之四:Method Swizzling
- UiObject2介绍
- HowTo Perform the spatial selection 'Share a line segment with' using ArcObjects
- iOS 7应用开发公开课笔记 L3:Objective-C
- android 中Jsonobject里的键值是动态时,获得键值key
- Objective-C Block的实现
- Objective-C学习-NSSet(集合),NSMutableSet(可变集合) NSCountSet(计数集合)
- Objective-C:神在细节之中
- Objective-C Associated Objects 的实现原理
- Objective-C 的动态提示和技巧
- Core Data浅谈系列之三 : 了解NSManagedObject和NSPredicate(附Demo)
- 问题1 String类和Object类中的equals方法比较
- 【转载】Animation 动画(三)ObjectAnimator、valueAnimator
- Objective-C 中的元类(meta class)
- [iOS]Objective-C的语言版本,与Xcode版本有对应关系吗?