如何使用 JavaScript 在数组上同时使用映射和过滤器?
filter() 方法用于过滤数组中的值,map() 方法用于根据旧数组的每个值将新值映射到另一个数组。
有时,我们需要同时使用filter()和map()方法。例如,我们想从数组中过滤掉所有正数,并将它们的对数值映射到新数组
在开始本教程之前,我们先来看看filter()和map()方法的介绍。在本教程中,我们将学习如何使用 JavaScript 在数组上同时使用映射和过滤方法。
array.filter()方法的语法
用户可以按照下面的语法来使用JavaScript的filter()方法。
array.filter((element, index, self) => { // write a condition to filter values. // based on the filtering condition, return a boolean value to include in the filtered array. })
在上面的语法中,我们将回调函数作为filter()方法的参数传递。
array.map()方法的语法
用户可以按照下面的语法来使用JavaScript的map()方法。
array.map((element, index, self) => { // perform some action on the element of the array // return element for a new array })
在上面的语法中,我们需要从map()方法的回调函数中返回数组元素。
参数
element – 使用 filter() 方法迭代数组时,它是数组的当前元素。
index – 它是数组中元素的索引。
self – 它本身就是一个数组。
同时使用 array.map() 和 array.filter() 方法
本节将教我们在单个数组上一起使用filter()和map()方法。
语法
用户可以按照以下语法一起使用map()和filter()方法。
let logarithmic_values = array.filter((element, index, self) => { // filtering condition }) .map((element, index, self) => { // return value from map method })
在上面的语法中,我们首先对数组使用了filter()方法,然后使用了map()方法。
示例 1
在下面的示例中,数组包含正数和负数。我们以数组为参考,并调用数组上的 filter() 方法来过滤数组中的所有正值。在filter()方法的回调函数中,如果数字大于零,我们返回true;否则错误。
之后,我们使用map()方法并返回每个过滤元素的对数。用户可以看到 logarithmic_values 数组仅包含六个值,因为我们已经删除了过滤数组中的所有负值。
<html> <body> <h3>Using the <i> array.map() and array.filter() </i> methods together in JavaScript</h3> <div id = "output"> </div> <script> let output = document.getElementById('output'); let array = [10, 20, -2, -4, 32, -6, -7, -8, 10, 11, -20] let logarithmic_values = array.filter((element, index, self) => { if (element > 0) { return true; } return false; }) .map((element, index, self) => { return Math.log(element); }) output.innerHTML += "The original array values are " + array + "<br/>"; output.innerHTML += "The logarithmic_values are " + logarithmic_values + "<br/>"; </script> </body> </html>
示例 2
在下面的示例中,我们创建了一个对象数组,数组中的每个对象都包含员工 ID、工作年限和工资。
之后,我们使用filter()方法过滤了所有经验超过3年的员工。接下来,我们使用map()方法将所有员工的工资增加50%,并将新工资存储在new_salaries数组中。
在输出中,用户可以初步观察增量后的工资总额。
<html> <body> <h2>Using the <i> array.map() and array.filter() </i> methods together in JavaScript </h2> <div id = "output"> </div> <script> let output = document.getElementById('output'); // Creating the array of objects let array = [{ emp_id: "01", experience: 3, salary: 50000 }, { emp_id: "02", experience: 7, salary: 30000 }, { emp_id: "03", experience: 6, salary: 20000 }, { emp_id: "04", experience: 5, salary: 10000 }, { emp_id: "05", experience: 3, salary: 5000 }, { emp_id: "06", experience: 2, salary: 40000 }, { emp_id: "07", experience: 1.5, salary: 60000 }, { emp_id: "08", experience: 2, salary: 70000 }] // use the forEach() loop method to find the total initial salary let total_initial_salary = 0; array.forEach((employee) => { total_initial_salary += employee.salary; }) // filter all employees with experience greater than 3 years. let new_salaries = array.filter((element) => { if (element.experience > 3) { return true; } return false; }) .map((element) => { // increase the salary of all employees by 50%, who have experienced greater than 3 years return element.salary * 0.5; }) // find the sum of new salaries let new_salary = total_initial_salary; let total_increased_salary = new_salaries.forEach((salary) => { new_salary += salary }) output.innerHTML += "The initial total salaries of all employees is " + total_initial_salary + "<br/>"; output.innerHTML += "The total salaries of all employees after increasing salaries for some employees is " + new_salary + "<br/>"; </script> </body> </html>
用户通过各种示例学习了如何一起使用filter()和map()方法。在第一个示例中,我们将 filter() 和 map() 方法与数字数组一起使用。在第二个示例中,我们还学习了如何将 filter() 和 map() 方法与对象数组一起使用。
以上是如何使用 JavaScript 在数组上同时使用映射和过滤器?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题











Python和JavaScript开发者的薪资没有绝对的高低,具体取决于技能和行业需求。1.Python在数据科学和机器学习领域可能薪资更高。2.JavaScript在前端和全栈开发中需求大,薪资也可观。3.影响因素包括经验、地理位置、公司规模和特定技能。

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。

如何在JavaScript中将具有相同ID的数组元素合并到一个对象中?在处理数据时,我们常常会遇到需要将具有相同ID�...

实现视差滚动和元素动画效果的探讨本文将探讨如何实现类似资生堂官网(https://www.shiseido.co.jp/sb/wonderland/)中�...

学习JavaScript不难,但有挑战。1)理解基础概念如变量、数据类型、函数等。2)掌握异步编程,通过事件循环实现。3)使用DOM操作和Promise处理异步请求。4)避免常见错误,使用调试技巧。5)优化性能,遵循最佳实践。

深入探讨console.log输出差异的根源本文将分析一段代码中console.log函数输出结果的差异,并解释其背后的原因。�...

探索前端中类似VSCode的面板拖拽调整功能的实现在前端开发中,如何实现类似于VSCode...
