JavaScript 如何从数组中删除假值

JavaScript 如何从数组中删除假值

假/错值: 在JavaScript中,有7个假值,如下所示

  • false
  • 零(0,-0)
  • 空字符串(“”,‘ ‘,
  • BigIntZero(0n,0x0n)
  • null
  • undefined
  • NaN

在JavaScript中,数组接受所有类型的假值。让我们看看如何从JavaScript数组中删除假值的一些方法:

  • 使用for-each循环
  • 使用Array.filter方法
  • 使用Array.reduce方法
  • 使用for…of循环

示例:

输入:[23, 0, “gfg”, false, true, NaN, 12, “hi”, undefined, [], “”]

输出:[23, “gfg”, true, 12, “hi”, []]

输入:[“”, 0, false, undefined, NaN, null]

输出:[]

方法: 有许多方法可以实现这一点,其中一些方法如下:

  • JavaScript的for..each循环
  • JavaScript的Array.filter()方法
  • ES6方式的Array.filter()方法
  • 传递布尔值
  • JavaScript的Array.reduce()方法
  • JavaScript的for…of循环

JavaScript的for..each循环: 在这种方法中,我们将使用for..each循环迭代数组,并在每次迭代时检查值是否为真,如果为真,则将该值推入一个新创建的数组中,然后返回新数组。

示例1

在此示例中,我们将使用Javascript的 forEach() 循环来删除虚假值。

let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];
 
function removeFalsey(arr) {
    // newly created array
    let newArr = [];
 
    // Iterate the array using the forEach loop
    arr.forEach((k) => {
        // check for the truthy value
        if (k) {
            newArr.push(k);
        }
    });
    // return the new array
    return newArr;
}
console.log(removeFalsey(arr));

输出:

[23, "gfg", true, 12, "hi", []]

JavaScript Array.filter() 方法 :在这种方法中,我们使用了 array.filter 方法。filter 方法检查数组并过滤掉数组中的假值,并返回一个新的数组。

示例2

在这个示例中,我们将使用 Array.filter() 方法从数组中移除假值。

let arr = ["", 0, false, undefined, NaN, null];
 
function removeFalsey(arr) {
    // Applying the filter method on the array
    return arr.filter((k) => {
        // Checking if the value is truthy
        if (k) {
            return k;
        }
    });
}
console.log(removeFalsey(arr));

输出:

[]

ES6 方法之 Array.filter(): 如果你可以使用这个 ES6 句子。

示例3

在这个示例中,我们将会在 ES6 中使用 Javascript Array.filter() 方法。

let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];
 
function removeFalsey(arr) {
    // Return the first parameter of the callback function
    return arr.filter((val) => val);
}
 
console.log(removeFalsey(arr));

输出:

[23, "gfg", true, 12, "hi", []]

传递布尔值: 您还可以通过将布尔构造函数作为filter方法的参数来实现。

示例4

在这个示例中,我们将在filter方法的参数中使用一个布尔构造函数。

let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];
 
function removeFalsey(arr) {
    // Passing Boolean constructor inside filter
    return arr.filter(Boolean);
}
 
console.log(removeFalsey(arr));

输出:

[23, "gfg", true, 12, "hi", []]

JavaScript Array.reduce() 方法 使用 Array.reduce 方法,我们遍历数组并将累加器初始化为空数组,如果当前值不为假值,则返回累加器的连接值,否则仅返回累加器。

示例5

在此示例中,我们将使用Javascript的 Array.reduce() 方法从数组中移除假值。

let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];
 
function removeFalsey(arr) {
    return arr.reduce((acc, curr) => {
        // Check if the truthy then return concatenated value acc with curr.
        // else return only acc.
        if (curr) {
            return [...acc, curr];
        } else {
            return acc;
        }
    }, []); // Initialize with an empty array
}
 
console.log(removeFalsey(arr));

输出:

[23, "gfg", true, 12, "hi", []]

JavaScript for…of循环 使用 for…of循环 迭代数组并检查每个项是否为假值或真值。如果该项为真值,则将该项推送到新创建的数组中。

示例6

在这个示例中,我们将使用Javascript的 for..of循环 从数组中删除假值。

let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];
 
function removeFalsey(arr) {
 
    // Create a new array
    let output = [];
    for (x of arr) {
        if (x) {
 
            // Check if x is truthy
            output.push(x);
        }
    }
    return output;
}
 
console.log(removeFalsey(arr));

输出:

[23, "gfg", true, 12, "hi", []]

JavaScript for循环 使用 for循环 迭代数组并检查每个项是否为假或真。如果项为真,则将项推入新创建的数组中。

示例7

在此示例中,我们将使用Javascript for循环 从数组中删除虚假值。

let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];
 
function removeFalsey(arr) {
    // Create a new array
    let output = [];
    for (let i = 0; i < arr.length; i++) {
        if (arr[i]) {
            output.push(arr[i]);
        }
    }
    return output;
}
console.log(removeFalsey(arr));

输出:

[23, "gfg", true, 12, "hi", []]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程