/    Sign up×
Community /Pin to ProfileBookmark

How to create an array of arrays at runtime

Hi.. In javascript, I need to create an array of arrays at runtime.
I have a list of key values and every value must contains a list of field names.. example:
to the first array I need to add new code “cod1” that is a list itself where I have to add “field1″ ,”field2” ecc..
so.. at runtime execution I need to add dynamically codes and fields too..
how can I do that, please?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@PadonakMar 03.2014 — something like this

<i>
</i>&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8" /&gt;
&lt;title&gt;Title&lt;/title&gt;
&lt;style&gt;
body{padding-top:200px;}
body,input{text-align:center;}
#res{margin-top:15px;height:250px;width:300px;overflow:auto;display:inline-block;}
&lt;/style&gt;
&lt;script&gt;
var mycoll={};

function $(id){return document.getElementById(id);}

function addline(){
var a=arguments[0],
b=arguments[1],
c=Number(arguments[2]);
if(mycoll[a]!==undefined){alert('this name already exists! pick another one');return;}
else{
var temp=[];
for(var i=0; i&lt;c; i++){temp.push(b+i);}
mycoll[a]=temp;
showall();
}
}

function showall(){
var output='';
for(var i in mycoll){output+='&lt;a href="#null" title="click me" onclick="javascript:var str=mycoll[''+i+'']+' '+typeof mycoll[''+i+''];alert(str);"&gt;'+i+'&lt;/a&gt; =&gt; '+mycoll[i]+'&lt;br /&gt;';}
$('res').innerHTML=output;
}

window.onload=function(){
$('go').onclick=function(){
var n=$('name').value||false,
f=$('fieldname').value||false,
h=$('howmany').value||false,
x=/[d]{1,}/g.test(h);
if(n&amp;&amp;f&amp;&amp;h&amp;&amp;x){addline(n,f,h);}
else{
$('res').innerHTML='all the fields must be &lt;span style="color:Crimson"&gt;correctly&lt;/span&gt; filled';
if(!x){$('howmany').value='';}
return;
}
}
setTimeout(function(){$('name').focus();},1000);
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input id="name" placeholder="letters and numbers" type="text" /&gt;
&lt;input id="fieldname" placeholder="letters and numbers" type="text" /&gt;
&lt;input id="howmany" placeholder="only numbers" type="text" /&gt;
&lt;input id="go" type="button" value="Add" /&gt;
&lt;br /&gt;
&lt;div id="res"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @girtri spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...