As pointed out in other comments, relying on JSON.parse and JSON.stringify to perform a deep cloning is suboptimal (edge cases, performances). Better use the method provided by some library (e.g lodash's cloneDeep).
pulling in an entire library for a single method can also be a kind of suboptimal (versioning, security auditing, vendoring vs. managed dependencies). it's a lot of overhead when there might be a better solution in just not doing deeply nested arrays.
My point is to use an optimised method. Deep cloning arrays or objects may not be very common but if the need arises, I'd recommend using an exisiting solution.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
As pointed out in other comments, relying on
JSON.parse
andJSON.stringify
to perform a deep cloning is suboptimal (edge cases, performances). Better use the method provided by some library (e.glodash
'scloneDeep
).Totally! JSON is the quick&dirty way 😂Lodash is definitely the preferred optimal solution. Thanks for pointing that out 👍
pulling in an entire library for a single method can also be a kind of suboptimal (versioning, security auditing, vendoring vs. managed dependencies). it's a lot of overhead when there might be a better solution in just not doing deeply nested arrays.
My point is to use an optimised method. Deep cloning arrays or objects may not be very common but if the need arises, I'd recommend using an exisiting solution.