From 588d551ba7b1aaa7b6f0bce630ba61c71804b768 Mon Sep 17 00:00:00 2001 From: Luan Muniz Date: Thu, 23 May 2019 14:13:46 -0300 Subject: [PATCH] Make loop stop when it doesn't have any more obj to check With this change, if you have and object like { foo: "bar" } and you do dlv(obj, 'foo.bar.foobar.0.whatever') it will run twice instead of 5 times. Signed-off-by: Luan Muniz --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 5d21277..50f7315 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ export default function dlv(obj, key, def, p, undef) { key = key.split ? key.split('.') : key; for (p = 0; p < key.length; p++) { obj = obj ? obj[key[p]] : undef; + if(obj === undef) return def; } return obj === undef ? def : obj; }