how to use the forown function from lodash in javascript

The forOwn function from Lodash iterates over all own properties of an object, executing a callback function on each iteration. Here's an example of how to use it:

const _ = require('lodash');

const obj = { a: 1, b: 'two', c: true };
_.forOwn(obj, function(value, key) {
  console.log(key + ': ' + value);
});

// Output:
// a: 1
// b: two
// c: true
188 chars
12 lines

In the example above, we imported Lodash as _, created an object called obj, and used _.forOwn to iterate over all properties of the object. The callback function we passed to forOwn takes two parameters, value and key, which represent the current value and key of the object being iterated over.

Inside the callback function, we simply logged out each property, along with its corresponding value.

Note that forOwn only iterates over own properties, which are properties that are directly defined on the object and not inherited from its prototype chain. If you also want to iterate over inherited properties, you can use the forIn function from Lodash instead.

gistlibby LogSnag