[英]Javascript array referencing an array position (not an element)
UPDATE: 更新:
Many asked why not using [arr[0], arr[1]]
. 许多人问为什么不使用
[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 . 问题是我必须将此数组传递给一个方法,我没有访问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. 我已经处理了
arr
数组,我不想处理pointer
数组以反映新数据,我已经知道它在哪里。
The Nina Scholz answer seems to solve the problem. Nina Scholz的答案似乎解决了这个问题。
Is there a way to use "pointers" like C in Javascript? 有没有办法在Javascript中使用像C这样的“指针”?
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: 这将引用
{prop: 3}
和{prop: 4}
对象,这不是我想要的,因为,如果我这样做:
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}
. 如您所见,
pointer
包含对象{prop:3}
和{prop:4}
的引用。
How can I achieve pointer
to hold reference to the position 0 of the array, instead of the object stored in it? 如何实现
pointer
来保持对数组位置0的引用,而不是存储在其中的对象? So, on this example, pointer => [{prop:1},{prop:3}]
? 那么,在这个例子中,
pointer => [{prop:1},{prop:3}]
?
I can't call pointer = [arr[0], arr[1]]
all the time because arr
will change constantly and asynchronously. 我不能一直调用
pointer = [arr[0], arr[1]]
,因为arr
将不断地和异步地改变。
Is there a "reactive" way to handle arrays? 是否存在处理数组的“反应”方式?
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. 如果指针可以指向不同的数组,则可以使
pointer
的元素成为包含对数组的引用及其索引的对象。
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. 有趣的是:几十年前,我在Symbolics Lisp Machines上使用了C实现。 This is basically how it represented C pointers.
这基本上就是它如何代表C指针。
You could use a getter function and return the element of the actual object. 您可以使用getter函数并返回实际对象的元素。
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 : 您可以使用具有get陷阱的代理 (IE不支持):
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.