2019/08/03 16:23
<p>JSでのnull判定とかundifined判定とか色々あるが、lodashのライブラリに便利なものがあったのでメモ。</p> <ul> <li>nullかどうかを判定</li> </ul><pre class="code JS" data-lang="JS" data-unlink>_.isNull(value)</pre> <ul> <li>null, およびundifined判定に使える</li> </ul><pre class="code JS" data-lang="JS" data-unlink>_.isNil(value)</pre><p><br /> 上二つのメソッドは、要はそれぞれ<code>val == null</code>,<code>val === null</code>の判定をしているということですね。<br /> DB上の値をフロントに返す際に、<a class="keyword" href="http://d.hatena.ne.jp/keyword/nil">nil</a>であれば何も返さないという実装にしていることがあるが、そうするとReact側で対応している変数には<code>undifined</code>が入ってしまう。<br /> そこで、null、もしくはundifinedかを検出できる2個目のメソッドが便利という話でした。</p><br /> <br /> <p>参考にしたサイト<br /> <iframe src="https://hatenablog-parts.com/embed?url=https%3A%2F%2Flodash.com%2Fdocs%2F4.17.15%23isNil" title="Lodash Documentation" class="embed-card embed-webcard" scrolling="no" frameborder="0" style="display: block; width: 100%; height: 155px; max-width: 500px; margin: 10px 0px;"></iframe><cite class="hatena-citation"><a href="https://lodash.com/docs/4.17.15#isNil">lodash.com</a></cite></p>