How to recursively assign values in JavaScript with @compactjs/assign.

You need to assign values from one object or array to another object or array? This is how you can easily to this with @compactjs/assign.

It works for both arrays and objects!

First install either with NPM:

npm i @compactjs/assign

And import it in your JavaScript:

import { assign } from '@compactjs/assign';

Or include it in your HTML via CDN:

<script src="https://unpkg.com/@compactjs/assign/dist/index.umd.js"></script>

Now you can easily assign values from one object/array to another:

const target = {
  boolean: true,
  string: 'abc',
  object: { number: 0, any: 0 },
};
const source = ['34', 'abc', [14, 2]];

assign(target, source);

target now looks like this:

{
  boolean: '34',
  string: 'abc',
  object: { number: 14, any: 2 },
}

For more info check out https://github.com/CompactJS/assign.