The assignin
function from lodash is used to assign properties of one or more source objects to a destination object in a shallow manner; that is, only the top-level properties are assigned and any nested properties are not merged. Here is how you can use the assignin
function in JavaScript:
index.tsx339 chars10 lines
In this example, we first require lodash using const _ = require('lodash');
. We then create three objects: sourceObj1
, sourceObj2
, and destObj
. The sourceObj1
and sourceObj2
objects contain the properties we want to assign to destObj
. The destObj
object has an existing name
property which will be overwritten by the value of name
property in sourceObj1
.
We then use the _.assignIn(destObj, sourceObj1, sourceObj2);
statement to assign the properties of the sourceObj1
and sourceObj2
objects to the destObj
. Since destObj
is the first argument in the assignIn
function, its properties will be overwritten by any matching properties of the source objects. The result is logged to the console using console.log(destObj);
.
This will output { name: 'John', age: 25, occupation: 'Programmer', language: 'JavaScript' }
, which shows that the properties from both source objects have been assigned to the destination object.
gistlibby LogSnag