Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kubrianity/javascript-reference-vs-copy
Comparing reference and copy in objects and arrays
https://github.com/kubrianity/javascript-reference-vs-copy
Last synced: about 1 month ago
JSON representation
Comparing reference and copy in objects and arrays
- Host: GitHub
- URL: https://github.com/kubrianity/javascript-reference-vs-copy
- Owner: Kubrianity
- Created: 2022-07-04T20:25:47.000Z (over 2 years ago)
- Default Branch: root
- Last Pushed: 2022-07-05T11:58:10.000Z (over 2 years ago)
- Last Synced: 2024-11-07T09:36:19.770Z (3 months ago)
- Language: HTML
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JavaScript 30 #14: JavaScript-Reference-vs-Copy
This is the fourteenth project of JavaScript30 Challenge.
This repo is for understanding difference between copying by value and reference in primitive data types, objects and arrays
## Features & Usage
- Array copy methods
```javascript
const copy = modelArray.slice();
const copy = [].concat(modelArray);
const copy = [...modelArray];
const copy = Array.from(modelArray);
```
- Object copy methods
```javascript
const copy = Object.assign({}, modelObject, {property : newValue});
const copy = {...modelObject};
```
- Usage of deep clone methods to copy deeper levels of the object
```javascript
const deepCopy = JSON.parse(JSON.stringify(modelObject));
//lodash library
const _ = require('lodash');
const deepCopy = _.cloneDeep(modelObject);
```