Giter VIP home page Giter VIP logo

Comments (8)

inetsurfer avatar inetsurfer commented on May 8, 2024 28

我的代码,还有更短的吗?
function bouncer(arr) { return arr.filter(Boolean); }

from freecodecamp.cn.

JinChengneng avatar JinChengneng commented on May 8, 2024 2

function bouncer(arr) { return arr.filter(function(val){ return Boolean(val); }); }

from freecodecamp.cn.

theRedHands avatar theRedHands commented on May 8, 2024 1

我太单纯,还到处找怎么判断NaN

from freecodecamp.cn.

huluoyang avatar huluoyang commented on May 8, 2024

先弄清filter的用法,课程下面有文档啊。
一句话就搞定弄这么复杂,没弄清原理啊。

from freecodecamp.cn.

Nydia07 avatar Nydia07 commented on May 8, 2024

请问能具体解释一下那个最短的代码吗??Boolean直接作为filter判断的参数是怎么回事??

from freecodecamp.cn.

S1ngS1ng avatar S1ngS1ng commented on May 8, 2024

@Nydia07 filter 接收一个回调函数作为参数,这个回调函数的返回值为 truefalse,用来决定元素是否保留。Boolean 是一个 function,返回值是 true 或者 false

filter 的回调函数有三个参数,按顺序分别是数组元素、index 和数组本身。而 Boolean 方法只接收一个参数。因此,这里就把数组元素传给了 Boolean(),然后返回 true 或者 false

from freecodecamp.cn.

seakingxc avatar seakingxc commented on May 8, 2024

请问能具体解释一下那个最短的代码吗??Boolean直接作为filter判断的参数是怎么回事??

new Boolean([value])
参数
value
可选,用来初始化 Boolean 对象的值。
描述Section
如果第一个参数不是布尔值,则会将其转换为布尔值。如果省略该参数,或者其值为 0、-0、null、false、NaN、undefined、或者空字符串(""),则生成的 Boolean 对象的值为 false。
不要用创建 Boolean 对象的方式将一个非布尔值转化成布尔值,直接将 Boolean 当做转换函数来使用即可:

var x = Boolean(expression); // 推荐
var x = new Boolean(expression); // 不太好
直接写成Boolean的原因是因为function即使没有定义形参,入参也是可以传的。

from freecodecamp.cn.

realcore110 avatar realcore110 commented on May 8, 2024

最简单做法 return arr.filter( item => {return item ; }

from freecodecamp.cn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.