如何在JavaScript中同时使用map和filter方法对数组进行操作

如何在JavaScript中同时使用map和filter方法对数组进行操作

filter()方法用于从数组中筛选值,而map()方法用于基于旧数组的每个值将新值映射到另一个数组。

有时候,我们需要同时使用filter()和map()方法。例如,我们希望从数组中筛选出所有正数,并将它们的对数值映射到新数组中。

在开始本教程之前,让我们先了解一下filter()和map()方法的介绍。在本教程中,我们将学习如何在JavaScript中同时使用map和filter方法对数组进行操作。

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()方法的参数传递。

语法 of array.map() method

用户可以按照以下的语法来使用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;否则返回false。

之后,我们使用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

在下面的示例中,我们创建了一个对象数组,数组中的每个对象包含员工编号、工作经验年数和工资。

然后,我们使用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()方法与对象数组一起使用。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程