The modern build of lodash's forOwn function for iterating over own enumerable object properties.
npx @tessl/cli install tessl/npm-lodash--forown@3.0.0The modern build of lodash's forOwn function as a standalone module for iterating over own enumerable properties of objects. This modularized version allows you to include only the specific lodash functionality you need, reducing bundle size and improving performance.
npm install lodash.forownvar forOwn = require('lodash.forown');For ES6+ environments (using CommonJS interop):
import forOwn from 'lodash.forown';Note: This package is CommonJS only and does not provide native ES6 module exports.
var forOwn = require('lodash.forown');
// Basic object iteration
var obj = { 'a': 1, 'b': 2, 'c': 3 };
forOwn(obj, function(value, key) {
console.log(key + ': ' + value);
});
// => logs 'a: 1', 'b: 2', 'c: 3' (iteration order is not guaranteed)
// Early exit by returning false
var users = {
'fred': { 'user': 'fred', 'active': false },
'barney': { 'user': 'barney', 'active': true }
};
forOwn(users, function(value, key) {
console.log(key);
if (key === 'fred') {
return false; // Exit iteration early
}
});
// => logs 'fred' only
// Using with context binding
function Logger(prefix) {
this.prefix = prefix;
}
Logger.prototype.log = function(value, key) {
console.log(this.prefix + key + ': ' + value);
};
var logger = new Logger('[LOG] ');
forOwn({ 'a': 1, 'b': 2 }, logger.log, logger);
// => logs '[LOG] a: 1', '[LOG] b: 2'Iterates over own enumerable properties of an object, invoking an iteratee function for each property.
/**
* Iterates over own enumerable properties of an object invoking `iteratee`
* for each property. The `iteratee` is bound to `thisArg` and invoked with
* three arguments: (value, key, object). Iteratee functions may exit iteration
* early by explicitly returning `false`.
*
* @param {Object} object The object to iterate over.
* @param {Function} [iteratee] The function invoked per iteration.
* @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {Object} Returns `object`.
*/
function forOwn(object, iteratee, thisArg)Parameters:
object (Object): The object to iterate overiteratee (Function, optional): The function invoked per iteration. Receives three arguments: (value, key, object). Defaults to identity function if not providedthisArg (*, optional): The this binding context for the iteratee functionReturns:
object parameterKey Features:
false from the iterateethisArg parameterUsage Examples:
// Simple property logging
var data = { name: 'John', age: 30, city: 'NYC' };
forOwn(data, function(value, key, obj) {
console.log('Property ' + key + ' has value ' + value);
});
// Conditional processing with early exit
var config = { debug: true, verbose: false, production: true };
forOwn(config, function(value, key) {
if (value === true) {
console.log('Enabled: ' + key);
if (key === 'production') {
return false; // Stop processing after finding production
}
}
});
// Object transformation (modifying original object)
var scores = { alice: '95', bob: '87', charlie: '92' };
forOwn(scores, function(value, key, obj) {
obj[key] = parseInt(value); // Convert strings to numbers
});
// scores is now { alice: 95, bob: 87, charlie: 92 }lodash._basefor, lodash._bindcallback, lodash.keys