JavaScript 如何在.map()中跳过一个元素
在JavaScript中,我们有时需要在使用map()方法时跳过数组元素。例如,我们需要在对元素进行一些数学运算后,将值从一个数组映射到另一个数组,但只有当数组值是有限的时才需要这样做。
在这种情况下,用户可以使用以下方法来在使用map()方法时跳过数组元素。
使用if-else语句
在array.map()方法中,我们可以使用if-else语句来跳过元素。如果元素满足if-else语句的条件,我们需要将元素返回进行映射;否则,我们可以返回一个null值。
语法
用户可以按照下面的语法使用if-else语句来在map()方法中跳过元素。
array.map((number) => {
if (condition) {
// return some value
}
return null;
})
在上述语法中,如果if-else语句的条件为真,我们返回某个值;否则,我们返回null。
示例1
在下面的示例中,我们创建了一个包含数字值的数组。我们的目标是将每个正数元素乘以2,并将其映射到乘法器数组中。在map()方法中,我们使用‘element > 0’条件来检查数组是否为正数,如果条件为真,则返回乘以2后的数字。
在输出中,用户可以看到当我们返回null值时,该数组索引显示为空。
<html>
<body>
<h2>Using the <i> if-else </i> statement to skip over element in the array.map() method</h2>
<div id = "output"></div>
</body>
<script>
let output = document.getElementById('output');
let array = [10, 20, 34, 4, 5, 6, 0, -1, 3, 2, 4, -2];
let multiplier = array.map((number) => {
if (number > 0) {
return number * 2;
} else {
return null;
}
})
output.innerHTML += "The final array after skipping the negative number in the map() method is - " + multiplier;
</script>
</html>
使用filter()方法与map()方法
我们可以在map()方法之前使用filter()方法。使用filter()方法,我们可以删除一些元素并在另一个数组中筛选所需元素。
在此之后,我们可以使用由filter()方法创建的数组来使用map()方法,通过这种方式,我们可以间接跳过map()方法中的元素。
语法
用户可以按照下面的语法来使用filter()方法在map()方法中跳过元素。
let filteredValues = array.filter((string) => {
// filtering condition
})
let str_array = filteredValues.map((element) => {
// map value
})
在上述语法中,首先,我们从数组中过滤值,并在过滤后的值上使用map()方法。
示例2
在下面的示例中,我们创建了一个包含不同字符串值的数组。我们的目标是将所有字符串转换为首字母大写的形式。因此,首先我们使用filter()方法来过滤所有首字母大写的字符串,并将它们存储在filteredValues数组中。
之后,我们将使用map()方法与filteredValues数组,将它们映射到一个新数组中,同时将它们转换为大写形式。
<html>
<body>
<h2>Using the <i> array.filter() </i> method to skip over element in the array.map() method.</h2>
<div id = "output"></div>
</body>
<script>
let output = document.getElementById('output');
let array = ["Hello", "hi", "JavaScript", "typescript", "C", "CPP", "html"];
let filteredValues = array.filter((string) => {
if (string.charCodeAt(0) > 96 && string.charCodeAt(0) < 123) {
return true;
}
return false;
})
let str_array = filteredValues.map((element) => {
return element.toUpperCase();
})
output.innerHTML += "The filtered values from the array are " + filteredValues + "<br/>";
output.innerHTML += "The final array after skipping some strings in the map() method is - " + str_array + "<br>";
</script>
</html>
使用数组的 reduce() 方法
map() 方法将元素映射到新数组。我们也可以使用 reduce() 方法实现相同的效果。我们可以使用一个空数组,并使用 reduce() 方法逐个将元素映射到该数组。
语法
用户可以按照以下语法使用 reduce() 方法,将其作为 map() 方法使用,并跳过一些元素。
let final_Array = numbers.reduce(function (array, element) {
if (condition) {
// perform some operation
// push the final element to the array
// return array
}
// return array
}, []);
在上述语法中,我们根据特定条件将元素推送到数组中;否则,我们返回不将元素推送到数组中以跳过元素的数组。
示例3
在下面的示例中,我们旨在将所有可以被2整除的元素映射到它们自己。因此,我们将回调函数作为reduce()方法的第一个参数,空数组作为第二个参数进行传递。
在回调函数中,如果条件满足,我们将元素推送到数组中并返回数组。否则,我们返回没有任何更改的数组。
最后,reduce()方法返回所有映射元素的数组,并将其存储在final_array变量中,用户可以在输出中看到。
<html>
<body>
<h2>Using the <i> array.reduce() </i> method to skip over element in the array.map() method.</h2>
<div id = "output"></div>
</body>
<script>
let output = document.getElementById('output');
let numbers = [10, 33, 34, 55, 57, 58, 90, 87, 85, 53];
let final_Array = numbers.reduce(function (array, element) {
if (element % 2 == 0) {
array.push(element);
}
return array;
}, []);
output.innerHTML += "The final array after skipping some strings in the map() method is - " + final_Array + "<br>";
</script>
</html>
我们学习了三种在map()方法中跳过元素的方法。第一种方法存储null元素,占用更多空间;第二种方法增加了时间复杂度,因为我们需要单独使用filter()方法。第三种方法是最好的选择,因为它既优化了空间又优化了时间。