+ 2
javascript foreach through array. Get previous item within a method?
array.forEach(si => si.update()); Is there a straightforward way for element si to use e.g the position of the previous element. I know I can pass index and array to a function called with forEach but I do not know if/how to do it for a method. Do I need to put the values into a global variable?
16 Réponses
+ 4
Not sure what was meant by "if/how to do it for a method", nor a particle system. This is just an idea.
https://code.sololearn.com/W2rZ18suN0K9/?ref=app
+ 5
bell how about this?
https://code.sololearn.com/cHJJJySSj4Wf/?ref=app
+ 4
array.forEach(elem, index => {
let prevElem = array[index -1];
});
+ 4
bell
No problem
And please don't mark any answer yet, keep the door open.
Happy experimenting 💪
+ 4
XXX thank you! my question is if there is a way whereby your call method could calculate Math.pow(this.id,prev.id)
+ 4
Here's what I wanted to do! Thank you to Ipang and XXX for providing codes. I was wondering if idx needs to be passed or the object could otherwise know its own position in array. my solutions with global variables were much more complicated than needed (as your codes have shown). now I can make plants!
https://code.sololearn.com/WfcZUUqHWj1p/?ref=app
+ 3
Ipang thank you, let me look carefully into your code. I was thinking in the direction of having a class managing objects of another class: class rope and class nodes, actually.
I think you keep a variable and I was wondering there was a way to know the position in array implicitly
+ 3
Ipang thank you for your code, I am going to get back to my rope code and see if I can move ahead with your suggestion. The question about what would be best practice is still open but time to experiment!
+ 3
Ipang i am grateful to all answers, yours does not close the matter but opens a door. That’s good enough for me!
+ 3
bell something like this?
https://code.sololearn.com/c6XQ8XRcL3BD/?ref=app
+ 3
yes but your code is using an external function. to combine properties of two objects. what I do not know if exists is a way to combine this. and other object within the method.
call in this case. like in cpp when you add two objects of vector class. what I want is to operate on the coordinates of an object relative to the previous one. In particular, to have a class rope managing objects of class node, for instance.
+ 3
Truly sorry, but I am not really able to understand what you mean by "class rope managing objects of class node" and "if exists is a way to combine this and the other object". How exactly are you going to check that?
Can you maybe make an example code😅? Anyways do you want something like this?
https://code.sololearn.com/cDoIKotAfl52/?ref=app
+ 3
bell oh now I understand what you were trying to say. You are right, global variables are mostly a bit confusing.
What you can do now in the update method on line 47, is that instead of taking idx as a parameter, take the object itself. This way, you can pass the previous object to the method using either my or Ipang's solution (I think Ipang's solution will be faster, not sure tho), as I showed in my latest code. So you wouldn't need the nodes array to be global.
Happy coding and good luck with your code!
+ 2
Krish [less active] thank you! your answer is then that I have to save the previous element in a global variable in order to pass it to the method (x.update calls a method for the class of x)? My question was if that is the only way or I can know within the method. My array is a particle system and I want to tie particles to each other. Would overwriting the global variable every time pose a bottleneck or even introduce runtime errors?
+ 2
you could add a property on each particule and maintain its value with a reference to the previous particule in the array...
for init / update (if order or content is modified):
array.forEach((e,i,a) => e.prevElem = i ? a[i -1] : null);
and when operating on a particule 'p' you could retrieve the previous particule by accessing p.prevPart.
You could also implement a getter wich dynamically find the particule stored at previous index, but that would be overkill compared to just access the i-1 index of your array in your forEach loop ;P
+ 2
visph thank you for your answer: indeed, what I am looking for is not a hack (there is always a way to manage ) but for a more “javascriptic” solution. if there is a easy access without extra variables.