how to use the extend function from lodash in javascript

The _.extend() function from lodash is used to copy all properties of a source object to a destination object. It can take multiple source objects as arguments and returns the destination object after all properties are copied.

Here's an example usage:

index.tsx
const _ = require('lodash');

const source1 = {name: 'John', age: 30};
const source2 = {address: '123 Main St'};
const dest = {name: 'Mike', gender: 'male'};

_.extend(dest, source1, source2);

console.log(dest); // {name: 'John', gender: 'male', age: 30, address: '123 Main St'}
280 chars
10 lines

In the example above, source1 and source2 are two objects whose properties we want to copy to the dest object. The _.extend() function is called with dest followed by source1 and source2 as arguments. The dest object is updated with properties from all source objects, and the updated object is returned.

Note that if multiple source objects have properties with the same name, the value of the last source object takes precedence.

gistlibby LogSnag