0

I have an array that holds 'particle' objects, with each particle taking parameters; x position, y position, angle of projection, and velocity.

I am trying to access the x and y positions for each particle within the array to perform further calculations, but I am having trouble with the syntax. Here is a brief summary of the code:

var Particle( x, y , angle, velocity) { 
// here the implementation of the dynamics of the particles are coded 
}
 
// here 100 random particle objects 
// are pushed to the array
 var particleArray = [];
 
for(var i =0; i < 100; i++){
particleArray.push(new Particle( 
 (Math.random()* ( innerWidth - radius*2) + radius), 
 (Math.random()* ( innerHeight - radius*2) + radius), 
 Math.PI*Math.random(), 5 )) 
}

now I want to try and access one of the components , for example: the x position of the 47th particle in the array, but I am having trouble like I said above with the syntax or if I have even approached this problem correctly.

The Doctor
6921 gold badge8 silver badges24 bronze badges
asked Jan 24, 2018 at 20:37
2
  • 2
    Where does the code at Question get an array element? Commented Jan 24, 2018 at 20:40
  • 4
    var Particle( x, y , angle, velocity) { is invalid syntax, try var Particle = function( x, y , angle, velocity) { Commented Jan 24, 2018 at 20:40

3 Answers 3

2

You can access the n-th object in the array via the square bracket notation (note that arrays are 0-indexed): [n-1].

Then you can access a certain property via the dot notation: object.property.

var x = particleArray[46].x
answered Jan 24, 2018 at 20:41

Comments

1

You should be able to access the x position 47th particle with particleArray[46].x. (Since arrays are "zero-indexed", the first particle is particleArray[0], the second is particleArray[1], etc.)

answered Jan 24, 2018 at 20:42

Comments

0

Here is a simple example.

Note that array in javascript are zero based index (first position is zero not one) hence 47th is index 46

var particles = [];
var innerWidth = 10;
var innerHeight = 5;
var radius = 2;
function Particle(x, y, angle, velocity){
 this.x = x;
 this.y = y;
 this.angle = angle;
 this.velocity = velocity;
}
function generateParticles(numberOfParticles){
 var tempParticles = [];
 for(var i = 0; i < numberOfParticles; i++){
 tempParticles.push(
 new Particle( 
 ( Math.random() * (innerWidth - radius * 2) + radius) ,
 ( Math.random() * ( innerHeight - radius * 2) + radius) ,
 Math.PI*Math.random(),
 5 
 )
 );
 }
 return tempParticles;
}
particles = generateParticles(100);
console.log(particles[46])

answered Jan 24, 2018 at 20:48

1 Comment

Thats similar to what I have, but i appreciate the help!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.