-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimmutable.js
More file actions
27 lines (19 loc) · 915 Bytes
/
immutable.js
File metadata and controls
27 lines (19 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/****************
* Immutable Data
***************/
//Name List or Object
const immutableNamesArray = ['Jane', 'Jack', 'Joe'];
const immutableNamesObject = { jane: 'Jane', jack: 'Jack', joe: 'Joe'};
//Create COPY to use
const namesArrayCopy = [...immutableNamesArray];
const namesObjectCopy = {...immutableNamesObject};
console.log(immutableNamesArray === namesArrayCopy); //false
console.log(immutableNamesObject === namesObjectCopy); //false
// modify the COPY
namesArrayCopy.push('Sam');
namesObjectCopy.jane = 'Sam'; //Set Jane to Sam (does NOT effect original)
// does NOT effects original and now you CAN trust your data
console.log(immutableNamesArray); //[ 'Jane', 'Jack', 'Joe']
console.log(namesArrayCopy); //[ 'Jane', 'Jack', 'Joe', 'Sam' ]
console.log(immutableNamesObject); //{ jane: 'Jane', jack: 'Jack', joe: 'Joe'}
console.log(namesObjectCopy); //{ jane: 'Sam', jack: 'Jack', joe: 'Joe'}