JS:仅针对非空和字符串值类型过滤数组

时间:2022-03-10 22:46:16

I am trying to filter an array like this:

我试图过滤这样的数组:

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

With this I want to filter all empty strings including undefined and null. Unfortunately my array have some arrays, which should not be there. So I need also to check only for string values and remove all other.

有了这个我想过滤所有空字符串,包括undefined和null。不幸的是我的数组有一些数组,不应该存在。所以我还需要检查字符串值并删除所有其他值。

How do I do that?

我怎么做?

5 个解决方案

#1


2  

You can check the type of the elements using typeof:

您可以使用typeof检查元素的类型:

array.filter(e => typeof e === 'string' && e !== '')

Since '' is falsy, you could simplify by just testing if e was truthy, though the above is more explicit

由于''是假的,你可以简单地通过测试e是否真实,尽管上面更明确

array.filter(e => typeof e === 'string' && e)

const array = [null, undefined, '', 'hello', '', 'world', 7, ['some', 'array'], null]

console.log(
  array.filter(e => typeof e === 'string' && e !== '')
)

#2


4  

You could check for a string and empty both in your filter method:

您可以在过滤器方法中检查字符串并清空它们:

array.filter(e => (typeof e === 'string') && !!e)

Note: !!e returns false if the element is null, undefined, '' or 0.

注意:!!如果元素为null,undefined,'或0,则返回false。

I should mention that the "arrow"-function syntax only works in browsers that support ES6 or higher.

我应该提到“箭头”功能语法仅适用于支持ES6或更高版本的浏览器。

The alternative is:

替代方案是:

array.filter(function(e) {
    return (typeof e === 'string') && !!e;
});

Note: Keep in mind that Array.prototype.filter doesn't exist in older browsers.

注意:请记住,旧版浏览器中不存在Array.prototype.filter。

#3


1  

>[1,,3,,5].filter(String)
[1,3,5]

> [1,,3,5] .filter(String)[1,3,5]

#4


0  

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

当在es6中返回由一行作为回调组成的方法时,不需要返回,因为这会隐式发生。

Hope this helps :-)

希望这可以帮助 :-)

let arr = ["", "", "fdsff", [], null, {}, undefined];

let filteredArr = arr.filter(item => (typeof item === "string" && !item) || !item)
                  
console.log(filteredArr)

#5


0  

const justStrings = array.filter(element => 
    (typeof element === 'string' || element instanceof String)
    && element
)

Explanation

To be shure your element is a string you have to check that it isn't a variable type (let str = 'hello') or an instance of String (new String('hello')) because this case would return object by typeof element.

要保证你的元素是一个字符串,你必须检查它不是一个变量类型(让str ='hello')或一个String实例(新的String('hello')),因为这种情况会按类型返回对象元件。

Additionally you have to check if your element exists.

此外,您必须检查您的元素是否存在。

#1


2  

You can check the type of the elements using typeof:

您可以使用typeof检查元素的类型:

array.filter(e => typeof e === 'string' && e !== '')

Since '' is falsy, you could simplify by just testing if e was truthy, though the above is more explicit

由于''是假的,你可以简单地通过测试e是否真实,尽管上面更明确

array.filter(e => typeof e === 'string' && e)

const array = [null, undefined, '', 'hello', '', 'world', 7, ['some', 'array'], null]

console.log(
  array.filter(e => typeof e === 'string' && e !== '')
)

#2


4  

You could check for a string and empty both in your filter method:

您可以在过滤器方法中检查字符串并清空它们:

array.filter(e => (typeof e === 'string') && !!e)

Note: !!e returns false if the element is null, undefined, '' or 0.

注意:!!如果元素为null,undefined,'或0,则返回false。

I should mention that the "arrow"-function syntax only works in browsers that support ES6 or higher.

我应该提到“箭头”功能语法仅适用于支持ES6或更高版本的浏览器。

The alternative is:

替代方案是:

array.filter(function(e) {
    return (typeof e === 'string') && !!e;
});

Note: Keep in mind that Array.prototype.filter doesn't exist in older browsers.

注意:请记住,旧版浏览器中不存在Array.prototype.filter。

#3


1  

>[1,,3,,5].filter(String)
[1,3,5]

> [1,,3,5] .filter(String)[1,3,5]

#4


0  

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

当在es6中返回由一行作为回调组成的方法时,不需要返回,因为这会隐式发生。

Hope this helps :-)

希望这可以帮助 :-)

let arr = ["", "", "fdsff", [], null, {}, undefined];

let filteredArr = arr.filter(item => (typeof item === "string" && !item) || !item)
                  
console.log(filteredArr)

#5


0  

const justStrings = array.filter(element => 
    (typeof element === 'string' || element instanceof String)
    && element
)

Explanation

To be shure your element is a string you have to check that it isn't a variable type (let str = 'hello') or an instance of String (new String('hello')) because this case would return object by typeof element.

要保证你的元素是一个字符串,你必须检查它不是一个变量类型(让str ='hello')或一个String实例(新的String('hello')),因为这种情况会按类型返回对象元件。

Additionally you have to check if your element exists.

此外,您必须检查您的元素是否存在。