@ianwalter/merge

Recursively merge JavaScript Objects

Usage no npm install needed!

<script type="module">
  import ianwalterMerge from 'https://cdn.skypack.dev/@ianwalter/merge';
</script>

README

@ianwalter/merge

Recursively merge JavaScript Objects

npm page CI

About

Inspired by and created as an alternative for deepmerge.

Usage

import merge from '@ianwalter/merge'

const item1 = { id: 1, details: { name: 'Civilian', lists: ['Folk'] } }
const item3 = { id: 2, details: { plays: 2 } }
const item2 = { id: 3, details: { lists: ['Chill', 'Alt'] } }

merge(item1, item2, item3) //=> {
//    id: 3,
//    details: {
//      name: 'Civilian',
//      plays: 2,
//      lists: ['Chill', 'Alt']
//    }
//  }

Note

Like Object.assign, merge will treat the first passed item as the "destination" and mutate it. If you want a new "destination" object simply pass an empty object {} as the first argument or you can always use @ianwalter/clone and clone the first argument beforehand. merge will also merge prototype properties of objects so if you don't want that you can also use clone to clone your objects before merging them since, with the default options, clone does not clone prototype properties.

Related

  • @ianwalter/clone - A configurable utility to clone JavaScript data (Objects, Arrays, etc)

License

Apache 2.0 with Commons Clause - See LICENSE

 

Created by Ian Walter