@writetome51/set-array
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

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

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

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

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

Installation

npm i @writetome51/set-array

Loading

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

Package Sidebar

Install

npm i @writetome51/set-array

Weekly Downloads

6

Version

2.0.2

License

MIT

Unpacked Size

3.1 kB

Total Files

5

Last publish

Collaborators

  • writetome51