how to use the assign function from the lodash library in typescript

To use the assign function from the lodash library in Typescript, we need to import the lodash library and assign function.

index.ts
import * as _ from 'lodash';

const obj1 = { a: 1 };
const obj2 = { b: 2 };

const result = _.assign(obj1, obj2);

console.log(result); // Output: { a: 1, b: 2 }
162 chars
9 lines

In the above example, we imported the entire lodash library as _. We then created two objects, obj1 and obj2. Finally, we used the _.assign() function to merge the two objects and store the result in result.

Note: If there are properties with the same name in both obj1 and obj2, the value from obj2 will overwrite the value in obj1.

gistlibby LogSnag