2

How to debug lodash chain functions in browser.

Ex:

if (_.size(_.values(_.omit(this.user, 'language')).filter(Boolean)) < 2)

If we want to debug _.omit(this.user, 'language') and then the end result with another function _.values() as shown in the example,what should be done.

I tried searching but can only find console.log but if we want to debug right in the browser how can we do it.

skyboyer
  • 22,209
  • 7
  • 57
  • 64

1 Answers1

5

Chain functions and lodash sequences are usually "debugged" or "tapped" into via _.tap or _.thru:

tap: This method invokes interceptor and returns value. The interceptor is invoked with one argument; (value). The purpose of this method is to "tap into" a method chain sequence in order to modify intermediate results.

so something like this:

const obj = { name: 'Ace', language: 'English', age: 3 }

const result = _(obj)
 .tap(x => console.log(x))
 .omit('language')
 .tap(x => console.log(x))
 .omit('age')
 .tap(x=> console.log(x))
 .value()
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

thru: This method is like _.tap except that it returns the result of interceptor. The purpose of this method is to "pass thru" values replacing intermediate results in a method chain sequence.

Akrion
  • 18,117
  • 1
  • 34
  • 54