2

In javascript we have array with static number of objects.

objectArray = [{}, {}, {}];

Can someone please explain to me how can I make this number dynamical?

                                                                   

                                      

                                      

3
  • add a dynamic amount of indexes. I'm not really sure what you're asking. Commented Apr 14, 2012 at 12:23
  • Why don't you read some documentation? developer.mozilla.org/en/JavaScript/Guide/… Commented Apr 14, 2012 at 12:42
  • I read, but another, thank you for this one) Commented Apr 14, 2012 at 12:48

2 Answers 2

7

You don't have to make it dynamic, it already is. You merely need to add more objects onto the array:

// Add some new objects
objectArray.push({});
objectArray.push({});
console.log(objectArray.length); // 5

// Remove the last one
objectArray.pop();
console.log(objectArray.length); // 4

In JavaScript, array lengths need not be declared. They're always dynamic.

You can modify the individual objects by array key:

// Add a property to the second object:
objectArray[1].newProperty = "a new property value!";
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to specify an array size when first creating the array if you don't want to. You can use:

var objectArray=new Array();

to create the array and add elements by:

objectArray[0] = "something";
objectArray[1] = "another thing";
objectArray[2] = "and so on";

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.