<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script type="text/javascript"> let arr = [1, 2, 3, 4, 5]; // 1.数组的filter方法: // 将满足条件的元素添加到一个新的数组中 let a=arr.filter(function(a,b,c) {//currentValue, currentIndex, currentArray if(a%2===0) { return true; } }); console.log(a); // 2.数组的map方法: // 将满足条件的元素映射到一个新的数组中 let newArray = arr.map(function (currentValue, currentIndex, currentArray) { // console.log(currentValue, currentIndex, currentArray); if(currentValue % 2 === 0){ return currentValue; } }); console.log(newArray); // [undefined, 2, undefined, 4, undefined] </script> </body> </html>
方法的实现: