Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
These can be achieved in several ways, but there might be some drawbacks: through JSON serialization/deserialization: const cloned = JSON.parse(JSON.stringify(array)); The advantage is that ...
Answer
#2: Post edited
- These can be achieved in several ways, but there might be some drawbacks:
- **through JSON serialization/deserialization:** `const cloned = JSON.parse(JSON.stringify(array));`- The advantage is that it works for any type of object, but [there are some drawbacks](https://medium.com/@pmzubar/why-json-parse-json-stringify-is-a-bad-practice-to-clone-an-object-in-javascript-b28ac5e36521) such as converting some values like `undefined` to empty objects `{}`.
- It also requires [custom code](https://stackoverflow.com/a/9382383/2780791) when dealing with cyclic references within the objects hierarchy.
- **using [Spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)**: `this.clonedArray = theArray.map(e => ({ ... e }));`- using a custom library such as [lodash](https://lodash.com/docs/#cloneDeep): `var deep = _.cloneDeep(array);`. It also support arbitrary objects, not just arrays.
- These can be achieved in several ways, but there might be some drawbacks:
- - **through JSON serialization/deserialization**:
- `const cloned = JSON.parse(JSON.stringify(array));`
- The advantage is that it works for any type of object, but [there are some drawbacks](https://medium.com/@pmzubar/why-json-parse-json-stringify-is-a-bad-practice-to-clone-an-object-in-javascript-b28ac5e36521) such as converting some values like `undefined` to empty objects `{}`.
- It also requires [custom code](https://stackoverflow.com/a/9382383/2780791) when dealing with cyclic references within the objects hierarchy.
- - **using [Spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)**:
- `this.clonedArray = theArray.map(e => ({ ... e }));`
- - **using a custom library such as [lodash](https://lodash.com/docs/#cloneDeep)**:
- `var deep = _.cloneDeep(array);`. It also support arbitrary objects, not just arrays.
#1: Initial revision
These can be achieved in several ways, but there might be some drawbacks: - **through JSON serialization/deserialization:** `const cloned = JSON.parse(JSON.stringify(array));` The advantage is that it works for any type of object, but [there are some drawbacks](https://medium.com/@pmzubar/why-json-parse-json-stringify-is-a-bad-practice-to-clone-an-object-in-javascript-b28ac5e36521) such as converting some values like `undefined` to empty objects `{}`. It also requires [custom code](https://stackoverflow.com/a/9382383/2780791) when dealing with cyclic references within the objects hierarchy. - **using [Spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)**: `this.clonedArray = theArray.map(e => ({ ... e }));` - using a custom library such as [lodash](https://lodash.com/docs/#cloneDeep): `var deep = _.cloneDeep(array);`. It also support arbitrary objects, not just arrays.