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?

4
  • can you narrow down your problem? its hard to understand Commented Jun 27, 2014 at 6: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. Commented Jun 27, 2014 at 6: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). Commented Jun 27, 2014 at 6: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 Commented Jun 27, 2014 at 6:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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...
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++) {

}

Comments

Your Answer

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.