how to use the extendown function from the underscore library in typescript

To use the extendown function from the Underscore library in TypeScript, first install the underscore library using npm:

index.ts
npm install underscore
23 chars
2 lines

Then, import the library in your TypeScript file:

index.ts
import * as _ from 'underscore';
33 chars
2 lines

Finally, you can use the extendown function to recursively extend a destination object with any number of source objects:

index.ts
const destination = {
  a: {
    b: 2,
  },
};
const source1 = {
  a: {
    c: 3,
  },
};
const source2 = {
  d: {
    e: 4,
  },
};

const result = _.extendown(destination, source1, source2);
console.log(result);
// Output: { a: { b: 2, c: 3 }, d: { e: 4 } }
260 chars
20 lines

In the example above, the destination object is extended with properties from source1 and source2. The resulting object is logged to the console:

index.ts
{
  a: {
    b: 2,
    c: 3,
  },
  d: {
    e: 4,
  },
}
58 chars
10 lines

Note that the extendown function recursively extends objects, meaning that it handles nested objects as well.

gistlibby LogSnag