如何使用 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() 方法的引數傳遞。
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;否則返回 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
在下面的示例中,我們建立了一個物件陣列,陣列的每個物件都包含員工 ID、工作經驗(以年為單位)和工資。
之後,我們使用 filter() 方法過濾所有工作經驗大於 3 年的員工。接下來,我們使用 map() 方法將所有員工的工資提高 50%,並將新的工資儲存在新工資陣列中。
在輸出中,使用者可以首先觀察到增量後的總工資之和。
<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() 方法。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP