npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

array-object-merge

v1.2.1

Published

Merge objects with arrays of objects based on given object key

Downloads

1,455

Readme

array-object-merge CircleCI Coverage Status

Merge two objects with arrays of objects based on given object key(s)

Merge two objects with arrays of objects based on given object key(s) (identifier). Logic from object-array-merge is modified to support extra functionalities, then wrapped with lodash.mergeWith as customizer.

Supports multiple arrays with one or more identical object keys. Note that one object key can be used as an identifier for multiple arrays, but each array can only have one unique object key that will be used as its identifier. Objects not having the key will be ignored.

The intended use case for this package is to update documents in document-based database (e.g. MongoDB) following the definition of PATCH HTTP verb. This package tries to extend PATCH definition to array of objects (as in the request body can contains only changes, instead of an entire content (i.e. PUT HTTP verb)).

Limitations (as of 1.2.0)

  • The result array currently is ordered alphanumerically according to the result of lodash.groupBy
  • Does not support arrays with both object and primitive elements; only primitive elements of the update object remains. (See Examples section for code sample).
    Array of objects only or primitive only still work as expected.

Usage

Installation

# NPM
npm i array-object-merge

# Yarn
yarn add array-object-merge

CommonJS

const merge = require('array-object-merge')

merge(original, update, identifiers)

ES6

import merge from 'array-object-merge'

merge(original, update, identifiers)

Test

Clone this repository to your local environment, then:

cd /path/to/local/array-object-merge

# NPM
npm install
npm test
npm run coverage # Optional

# Yarn
yarn install
yarn test
yarn run coverage # Optional

Examples

/**
 * Merge two objects with one identifier
 */
let original = {
  arr: [{ id: 1, key: 'value' }, { id: 2, key: 'value' }]
}
let update = {
  arr: [{ id: 2, key: 'newValue' }, { id: 3, key: 'value' }],
  field: 'newkey'
}

/**
 * output = {
 *   arr: [
 *     { id: 1, key: 'value' },
 *     { id: 2, key: 'newValue' },
 *     { id: 3, key: 'value' }
 *   ],
 *   field: 'newkey'
 * }
 */
let output = merge(original, update, 'id')


/**
 * Merge two objects with multiple arrays and identifiers
 */
let original = {
  arr1: [{ id: 1, key: 'value' }, { id: 2, key: 'value' }],
  arr2: [{ _id: 1, key: 'value' }, { _id: 2, key: 'value' }]
}
let update = {
  arr1: [{ id: 2, key: 'newValue' }],
  arr2: [{ _id: 1, key: 'newValue' }],
  field: 'newkey'
}

/**
 * output = {
 *   arr1: [{ id: 1, key: 'value' }, { id: 2, key: 'newValue' }],
 *   arr2: [{ _id: 1, key: 'newValue' }, { _id: 2, key: 'value' }],
 *   field: 'newkey'
 * }
 */
let output = merge(original, update, [ 'id', '_id' ])


/**
 * Merge two objects with arrays of primitive values
 */
let original = {
  arr: [1, 2, 3, 4]
}
let update = {
  arr: [5, 6, 7, 8]
}

/**
 * Per REST specification: the update array will replace the original one
 * output = {
 *   arr: [5, 6, 7, 8]
 * }
 */
let output = merge(original, update)


/**
 * Merge two objects with one identifier and primitive values
 */
let original = {
  arr: [{ id: 1, key: 'value' }, { id: 2, key: 'value' }, 'shouldbegone']
}
let update = {
  arr: [{ id: 2, key: 'newValue' }, { id: 3, key: 'value' }, 'shouldbeincluded'],
  field: 'newkey'
}

/**
 * output = {
 *   arr: [
 *     { id: 1, key: 'value' },
 *     { id: 2, key: 'newValue' },
 *     { id: 3, key: 'value' },
 *     'shouldbeincluded'
 *   ],
 *   field: 'newkey'
 * }
 */
let output = merge(original, update, 'id')

Credits

License

This library is under an MIT License.