objoin

1.2.1 • Public • Published

objoin

Join two objects together

Usage

const posts = [
  { authorId: 'id1', title: 'this is post 1' },
  { authorId: 'id2', title: 'this is post 2' },
  { authorId: 'id1', title: 'this is post 3' }
]

const users = {
  id1: { name: 'bob smith' },
  id2: { name: 'jane brown' }
}

objoin(posts, { key: 'authorId', set: 'author' }, (authorId, next) => {
  //authorIds are just unique Ids, so you don't have to fetch the same id multiple times
  //in this case, it would get called with authorId id1 and id2 (the second id1 would not be called)
  //normally this would be some call to the db or ajax call
  next(null, users[authorId]);
}, (err, obj) => {
  obj == [
    { authorId: 'id1', author: { name: 'bob smith' }, title: 'this is post 1' },
    { authorId: 'id2', author: { name: 'jane brown' }, title: 'this is post 2' },
    { authorId: 'id1', author: { name: 'bob smith' }, title: 'this is post 3' }
  ]
})

Readme

Keywords

none

Package Sidebar

Install

npm i objoin

Weekly Downloads

19

Version

1.2.1

License

MIT

Unpacked Size

13.1 kB

Total Files

7

Last publish

Collaborators

  • jga
  • dawnerd
  • ecwillis
  • orthagonal1
  • alaguna
  • aleperez92