array-hash-upsert
v1.0.2
Published
Add or update a value in an array/hash pair
Downloads
7
Readme
array-hash-upsert
Add or update a value in an array/hash pair
Install
$ npm install --save array-hash-upsert
Usage
var Upsert = require('array-hash-upsert')
var array = []
var hash = {}
var upsert = Upsert(array, hash)
upsert({id: 1})
upsert({id: 2})
upsert({id: 1, foo: 'bar'})
console.log(hash) // => {1: {id: 1, foo: 'bar'}, 2: {id: 2}}
console.log(array) // => [{id: 1, foo: 'bar'}, {id: 2}]
API
ArrayHashUpsert(array, hash, [idKey])
-> upsertFn
array
Required
Type: array
hash
Required
Type: object
idKey
Type: string
Default: 'id'
The key used to tell whether a value exists yet.
upsert(value)
value
Required
Type: object
Adds the value if its id is not in the hash.
Otherwise, mutates the existing object in the array/hash to match the passed in value.
Related / Recommended
License
MIT © Andrew Joslin