Javascript array referencing an array position (not an element) - Stack Overflow

UPDATE:Many asked why not using [arr[0], arr[1]]. The problem is I have to pass this array to a method,

UPDATE:

Many asked why not using [arr[0], arr[1]]. The problem is I have to pass this array to a method, which I don't have access Angular Material Table. And I don't want to call the method over and over again.

I already processed the arr array and I don't want to process pointer array to reflect the new data, which I already know where it is.

The Nina Scholz answer seems to solve the problem.


Is there a way to use "pointers" like C in Javascript?

What I want to do is:

I have an array with objects

const arr = [
    {prop: 3},
    {prop: 4},
];

And I want to have an array to point to the positions of this array

const pointer = [arr[0], arr[1]]; // I want pointer to point to be an array containing the first and second elements of arr

This will get a reference to the {prop: 3} and {prop: 4} objects, which is not what I want, because, if I do:

arr.splice(0, 0, {prop: 1}); // arr => [{prop:1},{prop:3},{prop:4}]
console.log(pointer); // [{prop: 3},{prop: 4}]

As you can see, pointer holds a reference to the objects {prop:3} and {prop:4}.

How can I achieve pointer to hold reference to the position 0 of the array, instead of the object stored in it? So, on this example, pointer => [{prop:1},{prop:3}]?

I can't call pointer = [arr[0], arr[1]] all the time because arr will change constantly and asynchronously.

Is there a "reactive" way to handle arrays?

UPDATE:

Many asked why not using [arr[0], arr[1]]. The problem is I have to pass this array to a method, which I don't have access Angular Material Table. And I don't want to call the method over and over again.

I already processed the arr array and I don't want to process pointer array to reflect the new data, which I already know where it is.

The Nina Scholz answer seems to solve the problem.


Is there a way to use "pointers" like C in Javascript?

What I want to do is:

I have an array with objects

const arr = [
    {prop: 3},
    {prop: 4},
];

And I want to have an array to point to the positions of this array

const pointer = [arr[0], arr[1]]; // I want pointer to point to be an array containing the first and second elements of arr

This will get a reference to the {prop: 3} and {prop: 4} objects, which is not what I want, because, if I do:

arr.splice(0, 0, {prop: 1}); // arr => [{prop:1},{prop:3},{prop:4}]
console.log(pointer); // [{prop: 3},{prop: 4}]

As you can see, pointer holds a reference to the objects {prop:3} and {prop:4}.

How can I achieve pointer to hold reference to the position 0 of the array, instead of the object stored in it? So, on this example, pointer => [{prop:1},{prop:3}]?

I can't call pointer = [arr[0], arr[1]] all the time because arr will change constantly and asynchronously.

Is there a "reactive" way to handle arrays?

Share Improve this question edited Dec 31, 2018 at 20:57 Gustavo Lopes asked Dec 31, 2018 at 20:34 Gustavo LopesGustavo Lopes 4,2144 gold badges21 silver badges62 bronze badges 6
  • Possible duplicate stackoverflow./questions/17382427/… – ClickThisNick Commented Dec 31, 2018 at 20:37
  • 3 Possible duplicate of Are there pointers in javascript? – Laurens Deprost Commented Dec 31, 2018 at 20:38
  • 2 Are your pointers always to the same array? In that case, just make an array of indexes. – Barmar Commented Dec 31, 2018 at 20:40
  • 1 @LaurensDeprost I don't think any of the answers there would help with this. – Barmar Commented Dec 31, 2018 at 20:42
  • 1 What are you trying to achieve with this? If you want the object at index 0, just use arr[0]... it will return the current value at the time. – Herohtar Commented Dec 31, 2018 at 20:42
 |  Show 1 more ment

3 Answers 3

Reset to default 5

If your pointers are always to the same array, you can simply store the indexes.

const pointer = [0, 1];

Then you would use:

console.log(pointer.map(ptr => arr[ptr]));

If your pointers can point to different arrays, you can make the elements of pointer be objects that contain references to the array along with their indexes.

const pointer = [{a: arr, i: 0}, {a: arr1, i: 1}];
console.log(pointer.map(({a, i}) => a[i]));

Interesting aside: several decades ago I used a C implementation for Symbolics Lisp Machines. This is basically how it represented C pointers.

You could use a getter function and return the element of the actual object.

const arr = [{ prop: 3 }, { prop: 4 }];
const pointer = [];

Object.defineProperty(pointer, 0, { get() { return arr[0]; } });
Object.defineProperty(pointer, 1, { get() { return arr[1]; } });

arr.splice(0, 0, { prop: 1 });
console.log(pointer);

You can use a Proxy (not supported by IE) with a get trap:

const arr = [{ prop: 3 }, { prop: 4 }];

const pointer = new Proxy([], {
  get(target, prop, receiver) {
    // if the prop is a string that can be converted to a number
    // return the corresponding value from the arr
    if(typeof prop === 'string' && !isNaN(Number(prop))) return arr[target[prop]];

    return Reflect.get(target, prop, receiver);
  }
});

pointer.push(0, 1);
console.log(pointer);

arr.splice(0, 0, { prop: 1 });
console.log(pointer);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744359036a4570377.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信