JS: Filter array only for non-empty and type of string values

后端 未结 4 1230
醉酒成梦
醉酒成梦 2021-01-21 21:52

I am trying to filter an array like this:

array.filter(e => { return e })

With this I want to filter all empty strings including undef

4条回答
  •  借酒劲吻你
    2021-01-21 22:35

    When returning a method that consists of one line as a callback in es6 there is no need for return as this happens implicitly.

    Hope this helps :-)

    let arr = ["", "", "fdsff", [], null, {}, undefined];
    
    let filteredArr = arr.filter(item => (typeof item === "string" && !item) || !item)
                      
    console.log(filteredArr)

提交回复
热议问题