ES5 => 筛选功能 Array.prototypefilter():
代码:
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter(word => word.length > 6); console.log(result); // expected output: Array ["exuberant", "destruction", "present"]
ES5 => 数组的处理 Array.prototype.map()
代码:
var array1 = [1, 4, 9, 16]; // pass a function to map const map1 = array1.map(x => x * 2); console.log(map1); // expected output: Array [2, 8, 18, 32]
本文通过实例演示了JavaScript中数组的filter和map方法的使用。filter用于筛选数组中符合条件的元素,而map则用于将数组中的每个元素转换为新形式。两个方法均提供了强大的数据处理能力。

被折叠的 条评论
为什么被折叠?



