I would like to remove all falsy values from an array. Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.
我想从数组中删除所有的falsy值。JavaScript中的Falsy值为false、null、0、""、undefined和NaN。
function bouncer(arr) {
arr = arr.filter(function (n) {
return (n !== undefined && n !== null && n !== false && n !== 0 && n !== "" && isNaN()!=NaN); });
return arr;
}
bouncer([7, "ate", "", false, 9, NaN], "");
The above is getting satisfied for all except the NaN test case. Can someone help me check in the array whether it contains NaN or not?
除了NaN测试用例之外,上面的所有测试都得到了满足。有人能帮我检查一下数组是否包含NaN吗?
15 个解决方案
#1
62
You can use Boolean :
您可以使用布尔:
var myFilterArray = myArray.filter(Boolean);
#2
47
Since you want to get rid of "falsy" values, just let JavaScript do its thing:
既然您想要去掉“falsy”值,那么就让JavaScript来完成它的工作:
function bouncer(arr) {
return arr.filter(function(v) { return !!v; });
}
The double-application of the !
operator will make the filter callback return true
when the value is "truthy" and false
when it's "falsy".
双重应用!操作符将使过滤器回调在值为“truthy”时返回true,在值为“falsy”时返回false。
(Your code is calling isNaN()
but not passing it a value; that's why that test didn't work for you. The isNaN()
function returns true
if its parameter, when coerced to a number, is NaN
, and false
otherwise.)
(您的代码调用isNaN()但不传递它的值;这就是为什么测试对你不起作用的原因。isNaN()函数返回true,如果其参数在强制为数字时为NaN,则返回false。
edit — note that
编辑-请注意,
function bouncer(arr) {
return arr.filter(Boolean);
}
would work too as LoremIpsum notes in another answer, because the built-in Boolean constructor does pretty much the exact same thing as !!
.
也可以像LoremIpsum在另一个答案中指出的那样工作,因为内置的布尔构造函数所做的事情与!
#3
12
truthyArray = arr.filter(el => el)
^ that's how you do it
^你怎么做
#4
9
You use isNaN()
in wrong way. It should be something like following:
您使用isNaN()的方式是错误的。应该是这样的:
function bouncer(arr) {
return arr.filter(function (n) {
return n !== undefined && n !== null && n !== false && n !== 0 && n !== "" && !isNaN(n);
});
}
}
Also you can rewrite it:
你也可以重写它:
function bouncer( arr ){
return arr.filter( function( value ){
return value;
});
}
#5
3
I know this can be done using the arr.filter() method. But I prefer using the Boolean() function. Is clearer to me. Here's how I did it, although a little longer:
我知道这可以使用arr.filter()方法完成。但是我更喜欢使用Boolean()函数。对我来说是清晰的。我是这样做的,虽然时间有点长:
function bouncer(arr) {
// Don't show a false ID to this bouncer.
var falsy;
var trueArr = [];
for (i = 0; i < arr.length; i++) {
falsy = Boolean(arr[i]);
if (falsy === true) {
trueArr.push(arr[i]);
}
}
return trueArr;
}
bouncer([7, "ate", "", false, 9]);
// returns a new array that is filtered accordingly.
#6
3
This is another equivalent, but illustrative, solution:
这是另一个等价但具有说明性的解决方案:
function bouncer( arr ){
return arr.filter( function( value ){
return value ? true : false;
});
}
This code sample is illustrative because it indicates to a reader that the variable value
will be evaluated as truthy or falsey, and the anonymous function will return a boolean, either true
or false
, mapping to the evaluation of value
.
这个代码示例具有说明性,因为它向读者表明变量值将被评估为truthy或falsey,而匿名函数将返回一个布尔值(true或false),映射到值的评估。
For someone who is not familiar with this approach of removing values from an array based on their truthiness, or for someone who is not familiar with (or has not read the documentation on) the filter
function, this example is the most concise that still conveys the behavior of the filter
function.
对于那些不熟悉这种方法删除数组中的值基于他们的真实或者不熟悉的人(或没有阅读文档)过滤函数,本例中是最简洁,仍然传达了滤波函数的行为。
Of course, in your application you may opt for the more concise, yet less insightful, implementation:
当然,在您的应用程序中,您可以选择更简洁、但不那么有洞察力的实现:
function bouncer( arr ){
return arr.filter( function( value ){
return value;
});
}
#7
2
I think a better deal this way
我觉得这样更好
function bouncer(arr) {
arr = arr.filter(function(item) {
return item;
return arr;
bouncer([7, "ate", "", false, 9, NaN, undefined, 0]);
#8
2
bouncer function:
保镖功能:
function bouncer(arr) {
return arr.filter((val) => {
return !!val;
});
}
console.log(bouncer([7, "ate", "", false, 9]));
#9
2
Thanks for all working answers above. Here are 3 approaches to solve the problem. Third solution addressed problem by your approach @Vignesh.
感谢以上所有有效的答案。这里有三种方法可以解决这个问题。第三个解决方案通过您的方法@Vignesh解决问题。
1.
function bouncer(arr) {
return arr.filter( function( val ){
return val;
});
}
2.
function bouncer(arr) {
return arr.filter(Boolean);
}
3.
function bouncer(arr) {
return arr.filter(function(val){
return val !== false && val !== "" && !(Number.isNaN(val)) && val !==
undefined && val !== 0 && val !== null;
});
}
#10
0
function bouncer(arr) {
var result = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i]) {
result.push(arr[i]);
}
}
return result;
}
bouncer([7, "ate", "", false, 9]);
#11
0
function falsy(value) {
if (value) {
return value;
}
}
function bouncer(arr) {
var filter = arr.filter(falsy);
return filter;
}
bouncer([7, "ate", "", false, 9]);
#12
0
function removeFalsy(value){
var val = Boolean(value);
if(!val)
return false;
return true;
}
function bouncer(arr) {
return arr.filter(removeFalsy);
}
bouncer([7, "ate", "", false, 9]);
#13
0
function bouncer(arr) {
function filterFalse(value) {
var a = Boolean(value);
if (a === true) {
return a;
}
return a;
}
function filterArray(x) {
var y = filterFalse(x);
if (y) {
return true;
} else {
return false;
}
}
var newArr = arr.filter(filterArray);
return newArr;
}
bouncer([1, null, NaN, 2, undefined]);
#14
0
function bouncer(arr) {
// Don't show a false ID to this bouncer.
for (var i = 0; i < arr.length; i++) {
if (!arr[i]) {
arr.splice(i, 1);
i = i-1;
}
}
return arr;
}
bouncer([7, "ate", "", false, 9]);
#15
0
This is my idea...
这是我的想法…
function bouncer(arr) {
// Don't show a false ID to this bouncer.
var result = [];
function isGood(obj){
if(!Boolean(obj)){
return false;
} else {
return true;
}
}
for (var i=0; i < arr.length; i++){
if (isGood(arr[i]) === true){
result.push(arr[i]);
}
}
return result;
}
console.log(bouncer([7, "ate", "", false, 9]));
#1
62
You can use Boolean :
您可以使用布尔:
var myFilterArray = myArray.filter(Boolean);
#2
47
Since you want to get rid of "falsy" values, just let JavaScript do its thing:
既然您想要去掉“falsy”值,那么就让JavaScript来完成它的工作:
function bouncer(arr) {
return arr.filter(function(v) { return !!v; });
}
The double-application of the !
operator will make the filter callback return true
when the value is "truthy" and false
when it's "falsy".
双重应用!操作符将使过滤器回调在值为“truthy”时返回true,在值为“falsy”时返回false。
(Your code is calling isNaN()
but not passing it a value; that's why that test didn't work for you. The isNaN()
function returns true
if its parameter, when coerced to a number, is NaN
, and false
otherwise.)
(您的代码调用isNaN()但不传递它的值;这就是为什么测试对你不起作用的原因。isNaN()函数返回true,如果其参数在强制为数字时为NaN,则返回false。
edit — note that
编辑-请注意,
function bouncer(arr) {
return arr.filter(Boolean);
}
would work too as LoremIpsum notes in another answer, because the built-in Boolean constructor does pretty much the exact same thing as !!
.
也可以像LoremIpsum在另一个答案中指出的那样工作,因为内置的布尔构造函数所做的事情与!
#3
12
truthyArray = arr.filter(el => el)
^ that's how you do it
^你怎么做
#4
9
You use isNaN()
in wrong way. It should be something like following:
您使用isNaN()的方式是错误的。应该是这样的:
function bouncer(arr) {
return arr.filter(function (n) {
return n !== undefined && n !== null && n !== false && n !== 0 && n !== "" && !isNaN(n);
});
}
}
Also you can rewrite it:
你也可以重写它:
function bouncer( arr ){
return arr.filter( function( value ){
return value;
});
}
#5
3
I know this can be done using the arr.filter() method. But I prefer using the Boolean() function. Is clearer to me. Here's how I did it, although a little longer:
我知道这可以使用arr.filter()方法完成。但是我更喜欢使用Boolean()函数。对我来说是清晰的。我是这样做的,虽然时间有点长:
function bouncer(arr) {
// Don't show a false ID to this bouncer.
var falsy;
var trueArr = [];
for (i = 0; i < arr.length; i++) {
falsy = Boolean(arr[i]);
if (falsy === true) {
trueArr.push(arr[i]);
}
}
return trueArr;
}
bouncer([7, "ate", "", false, 9]);
// returns a new array that is filtered accordingly.
#6
3
This is another equivalent, but illustrative, solution:
这是另一个等价但具有说明性的解决方案:
function bouncer( arr ){
return arr.filter( function( value ){
return value ? true : false;
});
}
This code sample is illustrative because it indicates to a reader that the variable value
will be evaluated as truthy or falsey, and the anonymous function will return a boolean, either true
or false
, mapping to the evaluation of value
.
这个代码示例具有说明性,因为它向读者表明变量值将被评估为truthy或falsey,而匿名函数将返回一个布尔值(true或false),映射到值的评估。
For someone who is not familiar with this approach of removing values from an array based on their truthiness, or for someone who is not familiar with (or has not read the documentation on) the filter
function, this example is the most concise that still conveys the behavior of the filter
function.
对于那些不熟悉这种方法删除数组中的值基于他们的真实或者不熟悉的人(或没有阅读文档)过滤函数,本例中是最简洁,仍然传达了滤波函数的行为。
Of course, in your application you may opt for the more concise, yet less insightful, implementation:
当然,在您的应用程序中,您可以选择更简洁、但不那么有洞察力的实现:
function bouncer( arr ){
return arr.filter( function( value ){
return value;
});
}
#7
2
I think a better deal this way
我觉得这样更好
function bouncer(arr) {
arr = arr.filter(function(item) {
return item;
return arr;
bouncer([7, "ate", "", false, 9, NaN, undefined, 0]);
#8
2
bouncer function:
保镖功能:
function bouncer(arr) {
return arr.filter((val) => {
return !!val;
});
}
console.log(bouncer([7, "ate", "", false, 9]));
#9
2
Thanks for all working answers above. Here are 3 approaches to solve the problem. Third solution addressed problem by your approach @Vignesh.
感谢以上所有有效的答案。这里有三种方法可以解决这个问题。第三个解决方案通过您的方法@Vignesh解决问题。
1.
function bouncer(arr) {
return arr.filter( function( val ){
return val;
});
}
2.
function bouncer(arr) {
return arr.filter(Boolean);
}
3.
function bouncer(arr) {
return arr.filter(function(val){
return val !== false && val !== "" && !(Number.isNaN(val)) && val !==
undefined && val !== 0 && val !== null;
});
}
#10
0
function bouncer(arr) {
var result = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i]) {
result.push(arr[i]);
}
}
return result;
}
bouncer([7, "ate", "", false, 9]);
#11
0
function falsy(value) {
if (value) {
return value;
}
}
function bouncer(arr) {
var filter = arr.filter(falsy);
return filter;
}
bouncer([7, "ate", "", false, 9]);
#12
0
function removeFalsy(value){
var val = Boolean(value);
if(!val)
return false;
return true;
}
function bouncer(arr) {
return arr.filter(removeFalsy);
}
bouncer([7, "ate", "", false, 9]);
#13
0
function bouncer(arr) {
function filterFalse(value) {
var a = Boolean(value);
if (a === true) {
return a;
}
return a;
}
function filterArray(x) {
var y = filterFalse(x);
if (y) {
return true;
} else {
return false;
}
}
var newArr = arr.filter(filterArray);
return newArr;
}
bouncer([1, null, NaN, 2, undefined]);
#14
0
function bouncer(arr) {
// Don't show a false ID to this bouncer.
for (var i = 0; i < arr.length; i++) {
if (!arr[i]) {
arr.splice(i, 1);
i = i-1;
}
}
return arr;
}
bouncer([7, "ate", "", false, 9]);
#15
0
This is my idea...
这是我的想法…
function bouncer(arr) {
// Don't show a false ID to this bouncer.
var result = [];
function isGood(obj){
if(!Boolean(obj)){
return false;
} else {
return true;
}
}
for (var i=0; i < arr.length; i++){
if (isGood(arr[i]) === true){
result.push(arr[i]);
}
}
return result;
}
console.log(bouncer([7, "ate", "", false, 9]));