移除JavaScript数组中所有空索引


假设有一个数组包含一些空索引,需要移除这些空索引。

让我们看看输入输出场景:

假设有一个包含一些空索引的数组。现在我们需要排除它们,并返回只有真值元素。

Array = [22, 45, , 56, 71, , 10];
Output = [22, 45, 56, 71, 10] 

正如我们在输出中看到的,数组中空索引已被移除。我们可以使用多种方法来完成上述任务,让我们看看解决方法。

使用filter()方法

JavaScript中的filter()方法将创建一个包含满足函数给定条件的元素的新数组。此方法不会对空元素执行操作,也不会更改或修改现有数组。

语法

以下是JavaScript中filter()方法的语法:

array.filter(function(currentValue index, arr));

参数

  • function()参数是一个函数,用于迭代每个数组元素。

  • currentValue参数,这将是当前元素的值。

  • index参数,这将是当前元素的索引。

  • arr参数将是当前元素的数组。

filter()方法将返回一个包含满足函数中条件的元素的新数组。如果没有任何元素通过条件,则返回空数组。

示例

在下面的示例中,我们有一个包含空索引的数组。我们使用了filter()方法来移除空索引。我们调用了filter()方法并将一个函数传递给它。对于数组中函数的每次迭代,它将返回当前项。现在它将过滤所有假值(空索引)并返回一个包含数组中真值项的新数组。

<!DOCTYPE html>
<html>
<head>
   <title>Removing all the empty indices from array</title>
   <button onClick = "fun()">Click! </button>
   <p id = "para"> </p>
</head>
<body>
   <script>
      const array = ['Hello', 'Tutorialspoint', , , , 23, , 5, 7];
      function fun(){
         const res = array.filter(item => {
            return item;
         });
         document.getElementById("para").innerHTML = res;
      };
   </script>
</body>
</html>

在输出中,我们可以看到filter()方法过滤了所有空索引(假值)并返回所有真值。

示例:使用While循环

实现上述所需任务的另一种方法是使用while循环。

在下面的示例中:

  • 我们有一个包含一些空索引的数组。

  • 然后我们创建了一个空数组来推送将满足循环中条件的值。

  • 然后我们有一个while循环,并检查元素是否存在,并将它们推入新数组。

<!DOCTYPE html>
<html>
<head>
   <title>Removing all the empty indices from array</title>
   <button onClick = "func()"> Click here! </button>
   <p id = "para"></p>
</head>
<body>
   <script>
      const array = [, , , , 4, , 6, 22, , 7, 5,77];
      document.write("Existing array : " , array);
      function func(){
         const resArr = [];
         let i = 0;
         while (i < array.length) {
            i++;
            if (array[i]) {
               resArr.push(array[i]);
            }
         };
         document.getElementById("para").innerHTML = "After removing empty indices: " + resArr;
      };
   </script>
</body>
</html>

在输出中,我们可以看到数组中存在的元素被推入新数组。

使用forEach()方法

JavaScript中的forEach()方法对数组中的每个元素调用一个函数,此方法不会对空元素执行操作。

语法

以下是JavaScript中forEach()方法的语法,返回值为undefined。

array.forEach(function(currentValue, index, arr), thisValue)

参数

  • function参数将迭代每个数组元素。

  • currentValue是数组中当前元素的值。

  • index是当前元素的索引。

  • arr,此参数是当前元素的数组。

示例

在这个例子中:

  • 我们有一个包含空索引的数组。通过使用forEach()方法,我们已经从数组中移除了所有空索引。

  • 我们创建了一个空数组来存储非空元素。forEach()方法将遍历数组,对于每次迭代,它将检查当前元素是否为null

  • 如果元素不为null,则我们将元素推入空数组。

<!DOCTYPE html>
<html>
<head>
   <title>Removing all the empty indices from array</title>
   <button onClick = "func()"> Click me! </button>
   <p id = "para"></p>
</head>
<body>
   <script>
      const array = ['apple', , , 'ball', , , , , 93];
      const ResArray = [];
      function func(){
         array.forEach(item => {
            if (item !== null) {
               ResArray.push(item);
            }
         });
         document.getElementById("para").innerHTML = ResArray;
      };
   </script>
</body>
</html>

正如我们在输出中看到的,我们已经使用forEach()方法将所有非空索引推入空数组。

更新于:2022年12月19日

浏览量:1000+

启动你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.