javascript foreach through array. Get previous item within a method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

9th Jan 2021, 9:31 AM
bell
bell - avatar
16 Answers
+ 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
10th Jan 2021, 4:01 AM
Ipang
14th Jan 2021, 5:44 PM
XXX
XXX - avatar
+ 4
array.forEach(elem, index => { let prevElem = array[index -1]; });
9th Jan 2021, 10:50 AM
Krish
Krish - avatar
+ 4
bell No problem And please don't mark any answer yet, keep the door open. Happy experimenting 💪
14th Jan 2021, 1:22 PM
Ipang
+ 4
XXX thank you! my question is if there is a way whereby your call method could calculate Math.pow(this.id,prev.id)
14th Jan 2021, 6:56 PM
bell
bell - avatar
+ 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
15th Jan 2021, 6:24 AM
bell
bell - avatar
+ 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
10th Jan 2021, 9:57 AM
bell
bell - avatar
+ 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!
14th Jan 2021, 1:15 PM
bell
bell - avatar
+ 3
Ipang i am grateful to all answers, yours does not close the matter but opens a door. That’s good enough for me!
14th Jan 2021, 1:50 PM
bell
bell - avatar
14th Jan 2021, 7:16 PM
XXX
XXX - avatar
+ 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.
14th Jan 2021, 7:53 PM
bell
bell - avatar
+ 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
15th Jan 2021, 4:43 AM
XXX
XXX - avatar
+ 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!
15th Jan 2021, 7:15 AM
XXX
XXX - avatar
+ 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?
9th Jan 2021, 11:04 AM
bell
bell - avatar
+ 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
10th Jan 2021, 4:40 AM
visph
visph - avatar
+ 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.
10th Jan 2021, 9:55 AM
bell
bell - avatar