0

Is there a way to define array size inside objects in Java Script? Like we do in C or C++, when we want user to enter values in array, we define a size and populate it using indexes according to user. Display is done using something like n[4] (assuming n is an array.)

   function activity(id, name, description, prior, post, start, end, current, status) {
         this.id = id;          //activity id
         this.name = name;          //activity name
          this.description = description;   //activity description  
         **this.prior = prior[];            // prior activities
         this.post = post[];            //post activities**
         this.start = start;            //activity start date
         this.end = end;            //activity end date
        this.currentWork = currentWork;     //current work that has been done
        this.status = status;       //current status
   }

I want prior and post to be arrays of size 3. I am making 18 instances of above object, each one with different value. Tell me how to access these values how to enter values in this array?

pooja
  • 137
  • 1
  • 1
  • 7
  • can you narrow down your problem? its hard to understand – Just code Jun 27 '14 at 06:01
  • Nope, just `this.prior = []; this.post = [];` will work; there's no need to tell JavaScript how big you think the array is going to be. – Ja͢ck Jun 27 '14 at 06:03
  • You can not define a size for arrays in JavaScript. Arrays do not have a fixed space like in C and Java. However, you *can* preoccupy # of spaces with `undefined` with `new Array(num)`. – Derek 朕會功夫 Jun 27 '14 at 06:03
  • Why do you want fixed size arrays, you can use array.length to determine the length of the array, the array will grow dynamically as it grows, anyways if you still want a fixed size array, assign a constant value to vairable eg:- `length=3` , and now use this instead of array.length in loops condition etc, that way you will make sure you do not exceed the `3` limit – Mustafa sabir Jun 27 '14 at 06:05

2 Answers2

2

You can create an array of a specified size with:

this.prior = new Array(3);

However, Javascript arrays are not a fixed size -- if you assign beyond the end, it will automatically grow. So normally just just create an empty array:

this.prior = [];

and then assign elements or use this.prior.push(newElement) to add to it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thanks for this valuable suggestion.. please help on one more issue.. From the above code in problem, m creating 18 new objects like var activity1 = new activity(1, "one", "abcde", , , 27/6/2014 , 30/6/2014, 20 , "runnning"); IN above statement how to insert values for these arrays of post and prior... – pooja Jun 27 '14 at 09:22
0

see this

array in javascript grows dynamically so according to that post you can just store the length in some variable and use it as for looping. like this

var data = [];
var arrlength = 10 ; // user-defined length

for(var i = 0; i < arrlength; i++) {

}
Community
  • 1
  • 1
bumbumpaw
  • 2,522
  • 1
  • 24
  • 54