Dynamic Javascript Array

Dynamic Array is one where elements can be added dynamically.Below are some ways to add dynamic javascript array elements.

<html>
<head>
<script type="text/javascript">
var family=new Array();
function add() {
var str=prompt("Add your family member");
if(str!=='undefined' && str!=null && str!='')
  family.push(str);
return family;
}
</script>
</head>
<body>
<form>
<h1>Dynamic Javascript Array Examples</h1>
<input type="button" name="add_me" value="Add Member" onclick="return add();" class="input_button"/> 
<input type="button" name="show_value" value="Show Members" onclick="return alert(family);" class="input_button" /> 
</form>
</body>
</html>

From this examples you will definitely understand what is dynamic javascript array.

Below there are 3 input buttons.Click the first one and do what it ask.Once you added, members definitely added as elements of "family" variable.If you can see in DOM, family variable have elements. Other wise click on the Show Members button you will be alert the array elements.

If you have console window click to see the elements added by you. Clearly indicate that its dynamic array.

Objects or Hashes or Associative array Examples

A normal array have indexed keys. An object or hash have user defined keys.
Dynamic associative array means have to change the properties or methods.
Here we show the simple dynamic object example.

Input Key(Property or methods):  Input Values: 

If you have console window click to see the elements added by you. Clearly indicate that its dynamic objects.

Below are the code snippets

<html>
<head>
<script type="text/javascript">
var personal={};
function DOCID(id) {
if(id==false || id=='' || id==='undefined')
return false;
return document.getElementById(id);
}
function add_object() {
var key=(DOCID("object_inputkey").value!=='undefined' && DOCID("object_inputkey").value!=='')?DOCID("object_inputkey").value:'';
var val=(DOCID("object_inputvalues").value!=='undefined' && DOCID("object_inputvalues").value!=='')?DOCID("object_inputvalues").value:'';
if(key!='' && val!='') {
personal[key]=val;
return true;
}
return false;
}
</script>
</head>
<body>
<form>
<h1>Dynamic Javascript Object(Associative Array) Examples</h1>
Input Key(Property or methods):<input type="text" style="width: 150px;" value="" id="object_inputkey" name="object_inputkey"> 
Input Values:<input type="text" style="width: 150px;" value="" id="object_inputvalues" name="object_inputvalues"> 
<input type="button" name="add_me" value="Add Property or Methods" onclick="return add_object();" class="input_button" />
</form>
</body>
</html>

There will be many ways exist to add dynamic array elements.But i showed 2 examples of what is dynamic array or objects.