You can use the clone function from lodash library to create a shallow copy of an object or array.
Here's an example of how to use the clone
function:
index.tsx231 chars12 lines
In the code above, we first require the lodash
library. Then we create an object obj
with some properties, and an array arr
with some elements. We use the clone
function to create a shallow copy of obj
and arr
, which are assigned to copyObj
and copyArr
, respectively.
Now copyObj
and copyArr
are independent from obj
and arr
. If we modify any property or element in obj
or arr
, the copy versions will remain the same.
index.tsx178 chars8 lines
As you can see, modifying obj.a
or arr[1]
doesn't affect the properties or elements of copyObj
and copyArr
, respectively.
gistlibby LogSnag