UNPKG

tui-code-snippet

Version:
48 lines (42 loc) 1.35 kB
/** * @fileoverview Execute the provided callback once for each property of object which actually exist. * @author NHN FE Development Lab <dl_javascript@nhn.com> */ 'use strict'; /** * Execute the provided callback once for each property of object which actually exist. * If the callback function returns false, the loop will be stopped. * Callback function(iteratee) is invoked with three arguments: * 1) The value of the property * 2) The name of the property * 3) The object being traversed * @param {Object} obj The object that will be traversed * @param {function} iteratee Callback function * @param {Object} [context] Context(this) of callback function * @memberof module:collection * @example * // ES6 * import forEachOwnProperties from 'tui-code-snippet/collection/forEachOwnProperties'; * * // CommonJS * const forEachOwnProperties = require('tui-code-snippet/collection/forEachOwnProperties'); * * let sum = 0; * * forEachOwnProperties({a:1,b:2,c:3}, function(value){ * sum += value; * }); * alert(sum); // 6 */ function forEachOwnProperties(obj, iteratee, context) { var key; context = context || null; for (key in obj) { if (obj.hasOwnProperty(key)) { if (iteratee.call(context, obj[key], key, obj) === false) { break; } } } } module.exports = forEachOwnProperties;