-2

I'm new to JavaScript and I would like to run a function on every element of an array.

To be more specific, in my code, I have an onclick function. When I click on an element, I want 3 other elements to move. The problem is that only 1 element is moving for each click.

Here is my code:

var intersects = raycaster.intersectObjects(reel);
var intersects1 = raycaster.intersectObjects(rang1);
var intersects2 = raycaster.intersectObjects(rang2);
var intersects3 = raycaster.intersectObjects(rang3);
var intersects4 = raycaster.intersectObjects(rang4);
var inter1 = intersects1.join()
console.log(intersects2)

if (intersects.length > 0) {
    //console.log(intersects1)

    if (intersects[0].object.type === "Mesh") {
        var objinter = intersects1[0].object;
        //DEPLACEMENTS

        new TWEEN.Tween(intersects1[0].object.position).to({
                x: objinter.userData.x0,
                y: objinter.userData.y0,
                z: objinter.userData.z0
            }, 1000)
            .easing(TWEEN.Easing.Elastic.Out).start();

    }
};

Is it possible to call every element of the array intersects1 at once, and how would I do this?

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
ThomasSzy
  • 3
  • 1
  • 3
  • 2
    That's the definition of what `map` does. Look up the array's `map` function. – Carcigenicate Jun 21 '17 at 13:48
  • Where's the click handler? And which array do you want to run the function over? – Carcigenicate Jun 21 '17 at 13:49
  • 1
    @Carcigenicate Sounds like `.forEach` would be more appropriate here. – JLRishe Jun 21 '17 at 13:50
  • @JLRishe If the function acts purely on side effects, ya, it would be. I don't know if he needs any kind of return from the function though, so `map` is more general use. If he's just starting a tween, `foreach` would be a better choice, if he doesn't need any data back that resulted from starting the tween. – Carcigenicate Jun 21 '17 at 13:51
  • Ok, to be more precise, the element of my array are mesh, and my problem is : on the click, the element intersects1[0] is moved, on another click it is the element intersects1[1] etc... I wanted to now how to make every element moving on one click... – ThomasSzy Jun 21 '17 at 13:56
  • Why are some parts using `intersects` and some other `intersects1`? I'm trying to write an answer, but I don't understand what you're going for here. – Carcigenicate Jun 21 '17 at 14:07
  • intersects is used to detect when the mouse is over an element. And intersects1 is the array of the element to move when you click on the intersects element. What i want to know is if it is possible to write something like intersects1[0,1,2] to select many elements – ThomasSzy Jun 21 '17 at 14:10

1 Answers1

1

A map (or as @JLRishe, a foreach if you don't need to return anything from the loop) would be what you need here. Note a normal for loop would work here too, but a higher-order function would probably be the neatest way of achieving this:

if (intersects.length > 0) {
    //console.log(intersects1)

    // For each element of intersects1, do...
    // The current object being moved is called obj
    intersects1.foreach(function(obj) {
        if (obj.object.type === "Mesh") {
            var objinter = obj.object;

            new TWEEN.Tween(obj.object.position).to({
                    x: objinter.userData.x0,
                    y: objinter.userData.y0,
                    z: objinter.userData.z0
            }, 1000)
            .easing(TWEEN.Easing.Elastic.Out).start();
        }

    })

};

Note, I still don't entirely understand why you check intersects[0].object.type === "Mesh", but then do a tween on intersects1 instead. I ignored that part in the answer. I can update it if you clarify your intent.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117