Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript array referencing an array position (not an element)

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?

like image 954
Gustavo Lopes Avatar asked Dec 31 '18 20:12

Gustavo Lopes


People also ask

How do you check if an element is not present in an array in JavaScript?

JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive.

How do I check in JavaScript if a value exists at a certain array index?

The indexof() method in Javascript is one of the most convenient ways to find out whether a value exists in an array or not. The indexof() method works on the phenomenon of index numbers. This method returns the index of the array if found and returns -1 otherwise.

How do you find the position value of a array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found. The following illustrates the syntax of the indexOf() method.

How do you check if item does not exist in array?

You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.


1 Answers

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.

like image 50
Barmar Avatar answered Nov 11 '22 20:11

Barmar