What is for-own?
The for-own npm package is a utility for iterating over the own enumerable properties of an object. It provides a simple and efficient way to loop through an object's properties, executing a callback function for each property.
What are for-own's main functionalities?
Iterate over object properties
This feature allows you to iterate over the own enumerable properties of an object. The callback function receives the value and key of each property.
const forOwn = require('for-own');
const obj = { a: 1, b: 2, c: 3 };
forOwn(obj, function(value, key) {
console.log(key + ': ' + value);
});
Early exit from iteration
This feature allows you to exit the iteration early by returning false from the callback function. In this example, the iteration stops when the key 'b' is encountered.
const forOwn = require('for-own');
const obj = { a: 1, b: 2, c: 3 };
forOwn(obj, function(value, key) {
if (key === 'b') return false;
console.log(key + ': ' + value);
});
Other packages similar to for-own
lodash
Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It includes a `forOwn` method that provides similar functionality to the for-own package, but with additional features and optimizations.
underscore
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It includes a `_.each` method that can be used to iterate over object properties, similar to for-own.
object-keys
The object-keys package is a simple utility that provides a method to get the own enumerable property names of an object. While it doesn't provide iteration directly, it can be used in conjunction with a loop to achieve similar functionality to for-own.
for-own
Iterate over the own and inherited enumerable properties of an object, and return an object with properties that evaluate to true from the callback. Exit early by returning false
.
Install
Install with npm:
npm i for-own --save
Run tests
npm test
Usage
var forOwn = require('for-own');
var obj = {a: 'foo', b: 'bar', c: 'baz'};
var values = [];
var keys = [];
forOwn(obj, function (value, key, o) {
keys.push(key);
values.push(value);
});
console.log(keys);
console.log(values);
Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by verb-cli on September 20, 2014.