如何使用 JavaScript 在数组上同时使用映射和过滤器?

如何使用 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 在数组上同时使用映射和过滤器?的详细内容,更多请关注编程之家其它相关文章!

相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小