$ cnpm install object-loops
Functional methods like forEach, map, filter, and other Array methods for Objects in javascript
npm install object-loops
object-loops/<loop>var filter = require('object-loops/filter')
var forEach = require('object-loops/for-each')
var mapKeys = require('object-loops/map-keys')
var map = require('object-loops/map')
var reduce = require('object-loops/reduce')
//... and more
// usage
forEach({ x:10, y: 20 }, callback)
filter({ x:10, y: 20 }, callback)
mapKeys({ x:10, y: 20 }, callback)
map({ x:10, y: 20 }, callback)
reduce({ x:10, y: 20 }, callback)
object-loops/chainvar chain = require('object-loops/chain')
chain({ x:10, y: 20 })
.filter(callback)
.mapKeys(callback)
.map(callback)
.reduce(callback)
.toJSON() // must be called at the end to return modified object
require('object-loops')() // extends Object.prototype
obj.forEach(callback)
obj.filter(callback)
obj.mapKeys(callback)
obj.map(callback)
obj.reduce(callback)
Tests whether every value in the object passes the test implemented by the callback.
var every = require('object-loops/every')
var obj = {
foo: 10,
bar: 20,
baz: 30,
qux: 40,
}
var allGreaterThan25 = every(obj, function (val, key, obj) {
return val > 25
})
allGreaterThan25 // false
*/
Creates a new object with keys and values flipped.
var inverse = require('object-loops/inverse')
var obj = {
foo: 10,
bar: 20,
baz: 30
}
var inversedObj = inverse(obj)
inversedObj /* keys and vals are flipped
{
'10': 'foo',
'20': 'bar',
'30': 'baz'
}
*/
Creates a new object with all entries that pass the test implemented by the provided function.
var filter = require('object-loops/filter')
var obj = {
foo: 10,
bar: 20,
baz: 30,
qux: 40,
}
var filteredObj = filter(obj, function (val, key, obj) {
return val > 25
})
filteredObj /* Only has entries with vals greater than 25
{
baz: 30,
qux: 40
}
*/
Find the value of the the object that passes the test implemented by the callback.
val is returned (else undefined)var find = require('object-loops/find')
var obj = {
foo: 10,
bar: 20,
baz: 30,
qux: 40,
}
var key = find(obj, function (val, key, obj) {
return val > 25
})
key // 30
var notfound = find(obj, function (val, key, obj) {
return val > 100
})
notfound // undefined
*/
Find the key of the the object that passes the test implemented by the callback. Very similar to Array.prototype.findIndex
key is returned (else undefined)var findKey = require('object-loops/find-key')
var obj = {
foo: 10,
bar: 20,
baz: 30,
qux: 40,
}
var key = findKey(obj, function (val, key, obj) {
return val > 25
})
key // 'baz'
var notfound = findKey(obj, function (val, key, obj) {
return val > 100
})
notfound // undefined
*/
Executes a provided function once per each object value.
var forEach = require('object-loops/for-each')
var obj = {
foo: 10,
bar: 20,
baz: 30
}
var keyConcat = ''
var valSum = 0
forEach(obj, function (val, key, obj) {
keyConcat += key
valSum += val
})
keyConcat // = 'foobarbaz'
valSum // = 60
Equivalent to Object.keys. Implemented specifically for chain.
var chain = require('object-loops/chain')
var obj = {
foo: 10,
bar: 20,
baz: 30
}
chain(obj)
.keys()
.toJSON()
// ['foo', 'bar', 'baz']
Like to keys, but includes enumerable keys from the prototype chain.
var keysIn = require('object-loops/keys-in')
function Person (name) {
this.name = name
}
Person.prototype.getName = function () {
return this.name
}
var person = new Person('foo')
keysIn(obj)
// ['name', 'getName']
// for comparison, `keys` would return ['name']
Creates a new object with the results of calling a provided function on every key in the object.
var mapKeys = require('object-loops/map-keys')
var obj = {
foo: 10,
bar: 20,
baz: 30
}
var mappedObj = mapKeys(obj, function (key, val, obj) {
return key + 'New'
})
mappedObj /* Each key is concated w/ 'New'
{
fooNew: 10,
barNew: 20,
bazNew: 30
}
*/
Creates a new object with the results of calling a provided function on every value in the object.
var map = require('object-loops/map')
var obj = {
foo: 10,
bar: 20,
baz: 30
}
var mappedObj = map(obj, function (val, key, obj) {
return val*2
})
mappedObj /* Each val multiplied by 2
{
foo: 20,
bar: 40,
baz: 60
}
*/
Applies a function against an accumulator and each value of the object to reduce it to a single value.
var reduce = require('object-loops/reduce')
var obj = {
foo: 10,
bar: 20,
baz: 30
}
var sum = reduce(obj, function (prevVal, val, key, obj) {
return prevVal + val
})
sum // 60
Tests whether some value in the object passes the test implemented by the callback.
var some = require('object-loops/some')
var obj = {
foo: 10,
bar: 20,
baz: 30,
qux: 40,
}
var anyGreaterThan25 = some(obj, function (val, key, obj) {
return val > 25
})
anyGreaterThan25 // true
*/
Like to keys, but returns direct enumerable values.
var values = require('object-loops/values')
function Person (name) {
this.name = name
}
Person.prototype.getName = function () {
return this.name
}
var person = new Person('foo')
values(obj)
// ['foo']
// for comparison, `valuesIn` would return ['foo', Person.prototype.getName]
Like to keys, but returns direct enumerable values including prototype chain.
var valuesIn = require('object-loops/values-in')
function Person (name) {
this.name = name
}
Person.prototype.getName = function () {
return this.name
}
var person = new Person('foo')
valuesIn(obj)
// ['foo', Person.prototype.getName]
MIT
Copyright 2013 - present © cnpmjs.org | Home |