Reputation: 153
This is probably a dumb question but whatever.
If have two arrays like this:
let a = [0,1,2];
let b = [0,1,2,3,4,5];
Is it possible that I can make changes in one happen in the other array as if they are the same place in memory but different lengths like you could in C? (in javascript)
What i want:
a[0] = 1;
console.log(b);
//[1,1,2,3,4,5];
console.log(a);
//[1,1,2];
Upvotes: 0
Views: 36
Reputation: 664444
Not with plain arrays (unless using getters/setters or proxies to copy the values). However, a shared memory like in C is possible with typed arrays and their subarray
method:
const b = Uint8Array.from([0, 1, 2, 3, 4, 5]);
const a = b.subarray(0, 3);
console.log(a, b);
a[0] = 1;
console.log(a, b);
The downside (?) is that you can store only integers in the array, not objects or strings.
Upvotes: 1