forEach 用于迭代数组。这是一个简单的例子:
const numbers = [1, 2, 3, 4, 5, 6, 7]; numbers.forEach(function (el) { if (el % 2 === 0) { console.log(el); } });
现在让我们将 forEach 与对象数组一起使用:
const movies = [ { title: 'Indiana Jones', score: 77, }, { title: 'Star Trek', score: 94, }, { title: 'Deadpool', score: 79, }, ]; movies.forEach(function (movie) { console.log(`${movie.title} - ${movie.score}/100`); });
Map 使用对数组中每个元素调用回调的结果创建一个新数组。
首先,让我们迭代一个基本数组:
const lCase = ['jim', 'bob', 'abby']; const uCase = lCase.map(function (t) { return t.toUpperCase(); }); console.log(lCase, uCase);
现在,让我们迭代对象数组:
const movies = [ { title: 'Indiana Jones', score: 77, }, { title: 'Star Trek', score: 94, }, { title: 'Deadpool', score: 79, }, ]; const titles = movies.map(function (movie) { return movie.title; }); console.log(titles);
我们可以编写如下箭头函数来简化语法:
const square = (x) => { return x * x; }; const sum = (x, y) => { return x + y; }; const rollDie = () => { return Math.floor(Math.random() * 6) + 1; }; console.log(square(2), sum(2, 3)); console.log(rollDie());
我们还可以重新访问电影示例并使用箭头函数:
const movies = [ { title: 'Indiana Jones', score: 77, }, { title: 'Star Trek', score: 94, }, { title: 'Deadpool', score: 79, }, ]; const newMovies = movies.map((movie) => { return `${movie.title} = ${movie.score} / 10`; });
隐式返回是编写函数的另一种简写方式,一些示例包括:
const rollDie = () => Math.floor(Math.random() * 6) + 1; const isEven = (num) => num % 2 === 0;
x 毫秒后运行回调
console.log('Apears 1st'); // takes callback, then milliseconds setTimeout(() => { console.log('Apears 3rd'); }, 3000); console.log('Apears 2nd');
每 x 毫秒重复一次回调
setInterval(() => { console.log(Math.random()); }, 2000);
如果我们想让回调最终停止,我们可以这样做:
const id = setInterval(() => { console.log(Math.random()); }, 2000); clearInterval(id); // stops the loop
Filter 创建一个新数组,其中包含在回调函数中返回 true 的元素
const nums = [9, 8, 7, 6, 5, 4, 3, 2, 1]; const odds = nums.filter((n) => { return n % 2 === 1; // our callback returns true or false // if it returns true, n is added to the filtered array }); // [9, 7, 5, 3, 1] const smallNums = nums.filter((n) => n < 5); // [4, 3, 2, 1]
我们还可以过滤数组中的对象:
const movies = [ { title: 'Indiana Jones', score: 77, }, { title: 'Star Trek', score: 94, }, { title: 'Deadpool', score: 79, }, ]; const badMovies = movies.filter((movie) => { return movie.score < 80; }); console.log(badMovies);
const firstWords = ['dog', 'jello', 'log', 'bag', 'wag', 'cupcake']; words.some((word) => { return word.length > 4; }); // true words.some((word) => word[0] === 'Z'); // false words.some((word) => word.includes('cake')); // true
const words = ['dog', 'dig', 'log', 'bag', 'wag']; words.every((word) => { return word.length === 3; }); // true words.every((word) => word[0] === 'd'); // false words.every((w) => { return w[w.length - 1] === 'g'; }); // true
const prices = [9.99, 1.5, 19.99, 49.99, 30.5]; const total = prices.reduce((total, price) => { return total + price; }); const min = prices.reduce((min, price) => { return Math.min(min, price); }); console.log(min);
我们也可以使用对象数组来做到这一点:
const movies = [ { title: 'Indiana Jones', score: 77, }, { title: 'Star Trek', score: 94, }, { title: 'Deadpool', score: 79, }, ]; let bestMovie = movies.reduce((best, movie) => { if (movie.score > best.score) { return movie; } return best; }); console.log(bestMovie);
我们还可以设置reducer的初始值:
let nums = [1, 2, 3, 4, 5]; let maxPlus100 = nums.reduce((max, num) => { return (max += num); }, 100); console.log(maxPlus100); // 115
以上是JavaScript:forEach、map、箭头函数、setTimeout、setInterval、filter、some、every 和 reduce的详细内容。更多信息请关注PHP中文网其他相关文章!