JavaScript lodash の get を使って Cannot read property を避ける
存在しないオブジェクトのプロパティにアクセスした時には Cannot read property エラーになります。
const data = {
hoge: {
min: 0,
max: 999,
}
};
const result = data.foo.min;
lodash の get を使うと、存在しない場合は undefined を返してくれます。
const data = {
hoge: {
min: 0,
max: 999,
}
};
const result = _.get(data, 'foo.min'); //エラーにならない。undefined を返す
最近のコメント