Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@okaponta_

lodashの真偽値判定まとめ

lodashのさまざまな真偽値判定

lodashisXXXXを使ったりするのですが、undefinedのときtrueだっけ・・・?
と迷うことが多々あったのでまとめてみました。

検証バージョンは4.17.14です。

引数 引数(コード) _.isEmpty() _.isNaN() _.isNil() _.isNull() _.isUndefined()
空文字 '' true false false false false
半角スペース ' ' false false false false false
NaN NaN true true false false false
null null true false true true false
undefined undefined true false true false true
真偽値 true true false false false false
数字 0 true false false false false
文字 'hoge' false false false false false
配列 [0,1,2] false false false false false
空配列 [] true false false false false
オブジェクト {hoge:'hoge'} false false false false false
空のオブジェクト {} true false false false false

isEmpty使用するときは要注意ですね。。

おまけ

以下で確認しました。

this.checkFuncReturnValue('isEmpty', hoge => _.isEmpty(hoge));
this.checkFuncReturnValue('isNaN', hoge => _.isNaN(hoge));
this.checkFuncReturnValue('isNil', hoge => _.isNil(hoge));
this.checkFuncReturnValue('isNull', hoge => _.isNull(hoge));
this.checkFuncReturnValue('isUndefined', hoge => _.isUndefined(hoge));
checkFuncReturnValue(funcName, func) {
  console.log(funcName);
  console.log(' blank =>' + func(''));
  console.log(' halfSpace =>' + _.isEmpty(' '));
  console.log(' fullSpace =>' + _.isEmpty(' '));
  console.log(' NaN =>' + func(NaN));
  console.log(' null =>' + func(null));
  console.log(' undefined =>' + func(undefined));
  console.log(' true =>' + func(true));
  console.log(' number =>' + func(0));
  console.log(' string =>' + func('hoge'));
  console.log(' array =>' + func([0, 1, 2]));
  console.log(' emptyArray =>' + func([]));
  console.log(' object =>' + func({ hoge: 'hoge' }));
  console.log(' emptyObject =>' + func({}));
}
1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
okaponta_
twitter @okaponta_ フォロー待ってます!

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?