Skip to content

Assigns a new value to an array without breaking its memory reference

License

Notifications You must be signed in to change notification settings

writetome51/set-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

setArray(array, newValue: any[]): void

Sets array to a newValue without breaking its memory reference.
It's a much better alternative to doing this:

array = newValue; // this breaks the memory reference.

Examples

let arr = [1,2,3,4,5];
let arrCopy = arr;
arr = [3,4,5,6,7]; // But does arrCopy get updated too?

console.log(arrCopy); // (nope)
// [1,2,3,4,5]  
 
// But what if you need arrCopy to stay in-sync with arr? 
// Change the value of arr using setArray() instead:
arr = [1,2,3,4,5];
arrCopy = arr;
setArray(arr, [3,4,5,6,7]);

console.log(arrCopy); // Success
// [3,4,5,6,7]; 

Installation

npm i @writetome51/set-array

Loading

import { setArray } from '@writetome51/set-array';

About

Assigns a new value to an array without breaking its memory reference

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published