javascript - for loop to print out multiple arrays with similar names -
i've got multiple arrays, so:
bugnames0 = ["wasp", "roach", "stinkbug", "mantis"]; bugnames1 = ["hornet", "beetle", "ant", "termite"]; bugnames2 = ["gnat", "fly", "grub", "chigger"]; bugnames3 = ["flea", "bed-bug","maggots", "cricket"];
next have loop:
function bugloop() { (var i=0; < 4 ; i++){ console.log(bugnames0[i]); } }
that print first array console, or each individually if manually update number in array's name.
but there way more this? following code bit doesn't work, hope explains trying do:
for (var i=0, j=0; < 4; i++) { console.log(bugnames(i)[j]); } }
here represents bugname#, update through 0 - 3 loop runs, printing out first option of each array represented j.
goal outcome printed console be:
"wasp", "hornet", "gnat", "flea"
or that.
if possible solutions using vanilla js i'm working on project (self assigned exercise) i'm trying complete using vanilla. kind of force myself know language better exercise.
(also, i've been coding 4 months, sorry if noob question. couldn't find answer online anywhere, lots of loops on printing out arrays normally.)
you store 4 arrays 1 larger array (each bugnames array element within larger array). let's call bugcollection
:
bugcollection = [["wasp", "roach", "stinkbug", "mantis"], ["hornet", "beetle", "ant", "termite"], ["gnat", "fly", "grub", "chigger"], ["flea", "bed-bug","maggots", "cricket"]]
alternately, keep variable storage of these arrays , say:
bugcollection = [bugnames0, bugnames1, bugnames2, bugnames3]
then iterate through larger array, logging out index @ each.
var onefromeacharray = function(index) { (var = 0; < bugcollection.length; i++) { console.log(bugcollection[i][index]); } } onefromeacharray(0) // console logs 'wasp', 'hornet', 'gnat', 'flea'
Comments
Post a Comment