There are a lot of ways to remove elements from an array in JavaScript, but what’s the easiest way to remove all falsy values from an array? In order to answer that question we’ll take a close look at truthy versus falsy values and type coercion within the context of an algorithm scripting challenge.

有很多方法可以从JavaScript中删除数组中的元素,但是从数组中删除所有虚假值的最简单方法是什么? 为了回答这个问题,我们将在算法脚本挑战的背景下仔细研究真值与假值以及类型强制。

算法指令 (Algorithm instructions)

Remove all falsy values from an array.

从数组中删除所有伪造的值。

Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.

JavaScript中的false null falsenull0""undefinedNaN

Hint: Try converting each value to a Boolean.

提示:尝试将每个值转换为布尔值。

提供的测试用例 (Provided Test Cases)

  • bouncer([7, "ate", "", false, 9])should return [7, "ate", 9].

    bouncer([7, "ate", "", false, 9])应该返回[7, "ate", 9]

  • bouncer(["a", "b", "c"])should return ["a", "b", "c"].

    bouncer(["a", "b", "c"])应该返回["a", "b", "c"]

  • bouncer([false, null, 0, NaN, undefined, ""])should return [].

    bouncer([false, null, 0, NaN, undefined, ""])应该返回[]

  • bouncer([1, null, NaN, 2, undefined])should return [1, 2].

    bouncer([1, null, NaN, 2, undefined])应该返回[1, 2]

解决方案1:.filter()和Boolean() (Solution 1: .filter( ) and Boolean( ))

PEDAC (PEDAC)

Understanding the Problem: We have one input, an array. Our goal is to remove all the falsy values from the array then return the array.

了解问题 :我们只有一个输入,一个数组。 我们的目标是从数组中删除所有伪造的值,然后返回数组。

The good people at freeCodeCamp have told us that falsy values in JavaScript are false, null, 0, "", undefined, and NaN.

freeCodeCamp的好人告诉我们,JavaScript中的false值是falsenull0""undefinedNaN

They have also dropped a major hint for us! They suggest converting each value of the array into a boolean in order to accomplish this challenge. I think that’s a great hint!

他们也为我们提供了重要提示! 他们建议将数组的每个值转换为布尔值以完成此挑战。 我认为这是一个很好的提示!

Examples/Test Cases: Our provided test cases show us that if the input array only contains falsy values, then we should just return an empty array. That’s pretty straightforward.

示例/测试用例 :我们提供的测试用例向我们展示了,如果输入数组仅包含伪造的值,那么我们应该只返回一个空数组。 那很简单。

Data Structure: We are going to stick with arrays here.

数据结构 :我们将在这里坚持使用数组。

Let’s talk about .filter():

让我们来谈谈.filter()

.filter() creates a new array with all elements that pass the test implemented by the provided function.

.filter()创建一个新数组,其中所有元素都通过了由提供的函数实现的测试。

In other words, .filter() goes through each element in an array and preserves all the elements that pass a certain test. All the elements in the array that fail that test are filtered out — they’re removed.

换句话说, .filter()遍历数组中的每个元素,并保留所有通过特定测试的元素。 阵列中所有未通过测试的元素将被滤除-将其删除。

For example, if we had an array of numbers and we only wanted the numbers greater than 100, we could use .filter() to accomplish that:

例如,如果我们有一个数字数组,而只希望数字大于100,则可以使用.filter()来完成:

let numbers = [4, 56, 78, 99, 101, 150, 299, 300]numbers.filter(number => number > 100)// returns [ 101, 150, 299, 300 ]

Let’s talk about the hint of converting each element to a boolean. This is a good hint because we can use .filter() to return the array with only the truthy values.

让我们谈谈将每个元素转换为布尔值的提示。 这是一个很好的提示,因为我们可以使用.filter()返回仅包含真值的数组。

We’re going to accomplish that through JavaScript type conversion.

我们将通过JavaScript类型转换来实现。

JavaScript gives us useful functions to convert one data type to another. String() converts to a string, Number() converts to a number, and Boolean() converts to a boolean.

JavaScript为我们提供了有用的功能,可以将一种数据类型转换为另一种数据类型。 String()转换为字符串, Number()转换为数字,而Boolean()转换为布尔值。

For example:

例如:

String(1234)// returns "1234"
Number("47")// returns 47
Boolean("meow")// returns true

Boolean() is the function we’ll be implementing with this challenge. If the argument provided to Boolean() is truthy, then Boolean() will return true. If the argument provided to Boolean() is falsy, then Boolean() will return false.

Boolean()是我们将在此挑战中实现的功能。 如果提供给Boolean()的参数为true,则Boolean()将返回true. 如果提供给Boolean()的参数为falsy,则Boolean()将返回false

This is useful to us because we know from the instructions that only false, null, 0, "", undefined, and NaN are falsy in JavaScript. Every other value is truthy. Knowing that, if we convert each value in the input array to a boolean, we can remove all elements that evaluate to false, and that will satisfy the requirements for this challenge.

这对我们很有用,因为我们从指令中知道,在JavaScript中只有falsenull0""undefinedNaN是虚假的。 其他所有值都是真实的。 知道了,如果我们将输入数组中的每个值转换为布尔值,则可以删除所有评估为false元素,这些元素将满足此挑战的要求。

Algorithm:

算法

  1. Determine which values in arr are falsy.

    确定arr中的哪些值是虚假的。

  2. Remove all falsy values.删除所有虚假值。
  3. Return the new array that contains only truthy values.返回仅包含真实值的新数组。

Code: See below!

代码 :见下文!

Without comments and removing the local variable:

没有注释并删除局部变量:

If you have other solutions and/or suggestions, please share in the comments!

如果您有其他解决方案和/或建议,请分享评论!

本文是freeCodeCamp算法脚本系列文章的一部分。 (This article is a part of the series freeCodeCamp Algorithm Scripting.)

本文引用了freeCodeCamp基本算法脚本:Falsy Bouncer 。 (This article references freeCodeCamp Basic Algorithm Scripting: Falsy Bouncer.)

You can follow me on Medium, LinkedIn, and GitHub!

您可以在Medium , LinkedIn和GitHub上关注我!

翻译自: https://www.freecodecamp.org/news/how-to-remove-falsy-values-from-an-array-in-javascript-e623dbbd0ef2/

如何从JavaScript中的数组中删除虚假值相关推荐

  1. JavaScript - 移除数组中的空字符串元素

    移除数组中的空字符串元素 使用 filter 方法对数组进行拷贝,删除空字符串元素,保留其他元素(第 22 ~ 24 行): <!DOCTYPE html> <html>< ...

  2. JS JavaScript中去除数组中重复元素的方法

    JS JavaScript中去除数组中重复元素的方法 感觉比较好理解的3种方法,总结一下,大家共同学习 方法一: Array.prototype.method1 = function(){ var a ...

  3. C语言编程>第二十六周 ① 函数fun的功能是:将形参b所指数组中的前半部分元素的值和后半部分元素的值对换。形参n中存放数组中数据的个数,若n为奇数,则中间的元素不动。

    例题:函数fun的功能是:将形参b所指数组中的前半部分元素的值和后半部分元素的值对换.形参n中存放数组中数据的个数,若n为奇数,则中间的元素不动. 例如,若a所指数组中的数据依次为:11 22 33 ...

  4. JS中去除数组中重复元素的方法

    JS中去除数组中重复元素的方法 第一种方法:使用数组中的splice方法 splice():删除元素,并向数组添加新元素,并返回被删除的元素 function f1(){var _arr=[8,5,0 ...

  5. C++与C语言中有关数组中元素排序

    C++与C语言中有关数组中元素排序 C语言中 ​ #include<stdio.h> #define n 4 int main(){ int a[n]; int i,j,temp; for ...

  6. PHP中获取数组中单列的值

    PHP中获取数组中单列的值如下: 利用PHP中的数组函数 array_column():返回数组中某个单列的值.(PHP 5.5+适用) 语法: array_column(array,column_k ...

  7. python多维数组添加元素_numpy中三维数组中加入元素后的位置详解

    今天做数据处理时,遇到了从三维数组中批量加入二维数组的需求.其中三维数组在深度学习的特征数据处理时经常会使用到,所以读者有必要对该小知识点做到清楚了解并掌握.现对三维数组中的元素位置结合代码做详细归纳 ...

  8. 3.js中判断数组中是否存在某个对象/值,判断数组里的对象是否存在某个值 的五种方法 及应用场景|判断数组里有没有某对象,有不添加,没有则添加到数组

    3.js中判断数组中是否存在某个对象/值,判断数组里的对象是否存在某个值 的五种方法 及应用场景 一.当数组中的数据是简单类型时: 应用js中的indexof方法:存在则返回当前项索引,不存在则返回 ...

  9. 从PHP中的数组中删除元素

    有没有一种简单的方法可以使用PHP从数组中删除元素,以便foreach ($array)不再包含该元素? 我认为将其设置为null可以做到,但是显然不起作用. #1楼 如果您有一个数字索引的数组,其中 ...

最新文章

  1. [洛谷P1268]树的重量
  2. python实现简单的api接口-Python实现简单的API接口
  3. 第三十二讲 ASP.NET网络打印
  4. 新装ubuntu9.10后配置全过程(很多常见问题的解决方法)
  5. 关于linux系统下文件压缩归档操作命令略提
  6. KVM虚拟机添加硬盘
  7. 2019ICPC(上海) - Color Graph(二分图+状态压缩)
  8. 关于 IPv6 大规模部署,给我们带来了什么~
  9. 什么是数字光端机?数字光端机使用注意事项详解!
  10. C 文件读写 容易疏忽的一个问题
  11. Kinaba及X-Pack插件安装
  12. java 获取方法_Java 反射理解(三)-- Java获取方法信息
  13. 编译wide-dhcpv6-20080615报错问题
  14. mysql 5.7配置项最详细的解释
  15. 百面机器学习—6.PCA与LDA要点总结
  16. Android ssl 异常,SSL握手异常,同时通过https连接使用Android中的自签名证书Nougat
  17. matlab 四叉树表达,已知二值图像,如题图8.4所示。 (1)对该图像使用四叉树进行划分; (2)用四叉树表达该图像。 - 试题答案网问答...
  18. python编程题自动评分系统_基于数据分析的评分系统改进
  19. 对数幅度谱图像matlab,幅度谱 fft2绘制图像的对数幅度谱,比较图像旋转、平移和缩放后的频谱...
  20. 安装免费在线客服livezilla系统

热门文章

  1. Docker安装ElasticSearch和Kibana并解决常见问题
  2. 爬取顺企网商户联系方式
  3. 微信QQ已经被封了的域名怎么处理 微信QQ已经被封了的域名如何正常打开
  4. iOS开发 - 图片实现多层折叠效果
  5. MySQL数据库表字段命名规范
  6. unix linux模拟器 for windows(cygwin)
  7. 人机界面在石油钻井工程中的应用:如何搭建钻井工程参数监测系统?
  8. node与mySQL基本知识
  9. 高的上去,沉得下来!
  10. Vulnhub靶场渗透测试系列DC-2(wpscan使用和git提权)