Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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);
```