utility methods for looping through objects
why?
I got tired of typing Object.keys(object).forEach(function(key) { //do something with object[key] })
examples
import { forEachKeys, filterKeys, mapKeys, everyKeys, someKeys, reduceKeys, findKeys } from 'object-looper'
forEachKeys(obj, (key, value, index) => {
//do something
})
const obj = {
key1: 'test',
key2: 'test2'
}
const filtered = filterKeys(obj, (key, value, index) => {
return value === 'test'
})
//filtered is { key1: 'test' }
const obj = {
key1: 'test',
key2: 'test2'
}
const mapped = mapKeys(obj, (key, value, index) => {
return { [key]: value + 'map'}
})}
//mapped is now { key1: 'testmap', key2: 'test2map' }
const obj = {
key1: 1,
key2: 2
}
const isSmallEnough = everyKeys(obj, (key, value, index) => {
return value < 10
})
//isSmallEnough is true
const obj = {
key1: 1,
key2: 11
}
const isSmallEnough = someKeys(obj, (key, value, index) => {
return value < 10
})
//isSmallEnough is true
const obj = {
key1: 1,
key2: 11
}
const result = reduceKeys(obj, (prevKey, prevValue, currKey, currValue, index) => {
return prevValue + currValue
})
//result is 11
const obj = {
key1: 1,
key2: 11
}
const result = findKeys(obj, (key, value, index) => {
return value === 1
})
//result is { key1: 1 }
modules
you can also import each utility as its own module
import forEachKeys from 'object-looper/forEachKeys'
where is x method from array?
This initial release are methods that I think cover the majority of the use cases. please feel free to pull-request with added looping fun!