首頁  >  文章  >  web前端  >  如何使用 JavaScript 在陣列上同時使用映射和篩選器?

如何使用 JavaScript 在陣列上同時使用映射和篩選器?

PHPz
PHPz轉載
2023-08-28 20:29:05829瀏覽

如何使用 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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除