for循环打印出具有相似名称的多个数组

时间:2015-06-03 02:15:39

标签: javascript arrays loops for-loop console.log

我有多个阵列,如下:

bugNames0 = ["wasp", "roach", "stinkbug", "mantis"];
bugNames1 = ["hornet", "beetle", "ant", "termite"];
bugNames2 = ["gnat", "fly", "grub", "chigger"];
bugNames3 = ["flea", "bed-bug","maggots", "cricket"];

接下来我有这个循环:

function bugLoop() {
for (var i=0; i < 4 ; i++){
    console.log(bugNames0[i]);
    }
}

如果我手动更新数组名称中的数字,那么将成功将第一个阵列打印到控制台,或者单独打印每个阵列。

但有没有办法做更像这样的事情?以下代码位不起作用,但我希望它能解释我想要做的事情:

for (var i=0, j=0; i < 4; i++) {
    console.log(bugNames(i)[j]);
    }
}

这里我代表bugName#,我希望在循环运行时通过0-3更新,只打印出由j表示的每个数组的第一个选项。

打印到控制台的目标结果是:

"wasp", "hornet", "gnat", "flea"

或类似的东西。

如果可能的话,我希望解决方案只使用vanilla JS,因为我正在研究一个项目(自我分配的练习),我试图用vanilla来完成它。有点力量让自己更好地了解语言。

(另外,我只编写了4个月的代码,很抱歉,如果这是一个菜鸟问题。我无法在任何地方在线找到答案,通常会在打印阵列时出现很多循环。)

5 个答案:

答案 0 :(得分:2)

如果您可以将数组存储在数组中,那将是更好的选择。

例如:

bugNames[0] = ["wasp", "roach", "stinkbug", "mantis"];
bugNames[1] = ["hornet", "beetle", "ant", "termite"];
bugNames[2] = ["gnat", "fly", "grub", "chigger"];
bugNames[3] = ["flea", "bed-bug","maggots", "cricket"];

然后你可以正常遍历bugNames数组。

答案 1 :(得分:1)

您可以将所有四个数组存储到一个更大的数组中(每个bugNames数组只是这个更大数组中的一个元素)。我们称之为bugCollection

bugCollection = [["wasp", "roach", "stinkbug", "mantis"], ["hornet", "beetle", "ant", "termite"], ["gnat", "fly", "grub", "chigger"], ["flea", "bed-bug","maggots", "cricket"]]

或者,您可以保留这些数组的变量存储并说:

bugCollection = [bugNames0, bugNames1, bugNames2, bugNames3]

然后你可以遍历更大的数组,在每个数组中注销索引。

var oneFromEachArray = function(index) {
    for (var i = 0; i < bugCollection.length; i++) {
        console.log(bugCollection[i][index]);
    }
}

oneFromEachArray(0) // Console logs 'wasp', 'hornet', 'gnat', 'flea'

答案 2 :(得分:1)

您可以尝试使用eval

for (var j=0; j < 4 ; j++){
    for (var i=0; i < 4 ; i++){
        eval("console.log(bugNames" + j + "[i]);");
    }
}

答案 3 :(得分:1)

你可以像这样使用函数eval():

/**
 * @(#)Question3.java
 *
 *
 * @author 
 * @version 1.00 2015/5/22
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Question3 extends JPanel {
	
	private JPanel primary;
	private JRadioButton button1;
	private JRadioButton button2;
	private JRadioButton button3;
	private JRadioButton button4;
	private JLabel question1;
	private JLabel question2;
	private JLabel question3;
	private JLabel question4;
	private JLabel question5;
	private JLabel question6;
	private JLabel question7;
	private JLabel question8;
	private JLabel question9;
	private JLabel question10;
	private JRadioButton [][] Array1;
	private JLabel [][] Arrayq;

    public Question3() {
    	/*
    	Array1 = new JRadioButton[10][4];
    	Array1[1][0]=new JRadioButton ("A. Mesopotamian Civilization");
    	Array1[1][1]=new JRadioButton ("B. Egyptian Civilization");
    	Array1[1][2]=new JRadioButton ("C. Harrapan Civilization");
    	Array1[1][3]=new JRadioButton ("D. Chinese Civilization");
    	
    	Array1[2][0]=new JRadioButton ("A. Mesopotamian Civilization");
    	Array1[2][1]=new JRadioButton ("B. Egyptian Civilization");
    	Array1[2][2]=new JRadioButton ("C. Harappan Civilization");
    	Array1[2][3]=new JRadioButton ("D. Chinese Civilization");
    	
    	Array1[3][0]=new JRadioButton ("A. China");
    	Array1[3][1]=new JRadioButton ("B. India");
    	Array1[3][2]=new JRadioButton ("C. Irag");
    	Array1[3][3]=new JRadioButton ("D. Egypt");
    	
    	Array1[4][0]=new JRadioButton ("A. Aeschylus");
    	Array1[4][1]=new JRadioButton ("B. Sophocles");
    	Array1[4][2]=new JRadioButton ("C. Aristophanes");
    	Array1[4][3]=new JRadioButton ("D. Philip");
    	
    	Array1[5][0]=new JRadioButton ("A. 776 BC");
    	Array1[5][1]=new JRadioButton ("B. 780 BC");
    	Array1[5][2]=new JRadioButton ("C. 790 BC");
    	Array1[5][3]=new JRadioButton ("D. 800 BC");
    	
    	Array1[6][0]=new JRadioButton ("A. Euclid");
    	Array1[6][1]=new JRadioButton ("B. Pythagoras");
    	Array1[6][2]=new JRadioButton ("C. Hippocrates");
    	Array1[6][3]=new JRadioButton ("D. Erastosthenes");
    	
    	Array1[7][0]=new JRadioButton ("A. 1000 BC");
    	Array1[7][1]=new JRadioButton ("B. 1200 BC");
    	Array1[7][2]=new JRadioButton ("C. 1400 BC");
    	Array1[7][3]=new JRadioButton ("D. 1600 BC");
    	
    	Array1[8][0]=new JRadioButton ("A. Cicero");
    	Array1[8][1]=new JRadioButton ("B. Seneca");
    	Array1[8][2]=new JRadioButton ("C. Lucretius");
    	Array1[8][3]=new JRadioButton ("D. Octavian");
    	
    	Array1[9][0]=new JRadioButton ("A. Tacitus");
    	Array1[9][1]=new JRadioButton ("B. Plinky");
    	Array1[9][2]=new JRadioButton ("C. Virgil");
    	Array1[9][3]=new JRadioButton ("D. Marcus");
    	
    	Array1[10][0]=new JRadioButton ("A. 1285-90");
    	Array1[10][1]=new JRadioButton ("B. 1288-93");
    	Array1[10][2]=new JRadioButton ("C. 1290-96");
    	Array1[10][3]=new JRadioButton ("D. 1295-1301");
    	*/
    	//arrayq declaring
    	/*
    	Arrayq = new JLabel[10][1];
    	Arrayq[1][0]=new JLabel ("1. Rivers Tigris and Euphrates are associated with?");
    	Arrayq[2][0]=new JLabel ("2. Which is considered as oldest civilization of the world?");
    	Arrayq[3][0]=new JLabel ("3. Which among following is called \"Gift of the Nile\"?");
    	Arrayq[4][0]=new JLabel ("4. Who is considered as the master of Greek comedy?");
    	Arrayq[5][0]=new JLabel ("5. When ancient Olympic games first held?");
    	Arrayq[6][0]=new JLabel ("6. Who is known as the father of Modern Medicine?");
    	Arrayq[7][0]=new JLabel ("7. Rome was founded around?");
    	Arrayq[8][0]=new JLabel ("8. Which was not an Roman Philosopher?");
    	Arrayq[9][0]=new JLabel ("9. Who was among the famous Roman poets?");
    	Arrayq[10][0]=new JLabel ("10. Marco Polo, Venetian traveller, travelled from Venice to China and Japan in:");
    	*/
    	//question declaring
    	primary=new JPanel();
    	primary.setPreferredSize(new Dimension(100,400));
    	//font assigning
    	question1.setFont(new Font("Helvetica", Font.BOLD, 24));
    	question2.setFont(new Font("Helvetica", Font.BOLD, 24));
    	question3.setFont(new Font("Helvetica", Font.BOLD, 24));
    	question4.setFont(new Font("Helvetica", Font.BOLD, 24));
    	question5.setFont(new Font("Helvetica", Font.BOLD, 24));
    	question6.setFont(new Font("Helvetica", Font.BOLD, 24));
    	question7.setFont(new Font("Helvetica", Font.BOLD, 24));
    	question8.setFont(new Font("Helvetica", Font.BOLD, 24));
    	question9.setFont(new Font("Helvetica", Font.BOLD, 24));
    	question10.setFont(new Font("Helvetica", Font.BOLD, 24));
    	//button assigning
    	
    	button1=new JRadioButton("A. Mesopotamian Civilization",false);
    	button2=new JRadioButton("B. Egyptian Civilization",false);
    	button3=new JRadioButton("C. Harrapan Civilization",false);
    	button4=new JRadioButton("D. Chinese Civilization",false);
    	
    	button1=new JRadioButton("A. Mesopotamian Civilization",false);
    	button2=new JRadioButton("B. Egyptian Civilization",false);
    	button3=new JRadioButton("C. Harappan Civilization",false);
    	button4=new JRadioButton("D. Chinese Civilization",false);
    	
    	button1=new JRadioButton("A. China",false);
    	button2=new JRadioButton("B. India",false);
    	button3=new JRadioButton("C. Irag",false);
    	button4=new JRadioButton("D. Egypt",false);
    	
    	button1=new JRadioButton("A. Aeschylus",false);
    	button2=new JRadioButton("B. Sophocles",false);
    	button3=new JRadioButton("C. Aristophanes",false);
    	button4=new JRadioButton("D. Philip",false);
    	
    	button1=new JRadioButton("A. 776 BC",false);
    	button2=new JRadioButton("B. 780 BC",false);
    	button3=new JRadioButton("C. 790 BC",false);
    	button4=new JRadioButton("D. 800 BC",false);
    	
    	button1=new JRadioButton("A. Euclid",false);
    	button2=new JRadioButton("B. Pythagoras",false);
    	button3=new JRadioButton("C. Hippocrates",false);
    	button4=new JRadioButton("D. Erastosthenes",false);
    	
    	button1=new JRadioButton("A. 1000 BC",false);
    	button2=new JRadioButton("B. 1200 BC",false);
    	button3=new JRadioButton("C. 1400 BC",false);
    	button4=new JRadioButton("D. 1600 BC",false);
    	
    	button1=new JRadioButton("A. Cicero",false);
    	button2=new JRadioButton("B. Seneca",false);
    	button3=new JRadioButton("C. Lucretius",false);
    	button4=new JRadioButton("D. Octavian",false);
    	
    	button1=new JRadioButton("A. Tacitus",false);
    	button2=new JRadioButton("B. Plinky",false);
    	button3=new JRadioButton("C. Virgil",false);
    	button4=new JRadioButton("D. Marcus",false);
    	
    	button1=new JRadioButton("A. 1285-90",false);
    	button2=new JRadioButton("B. 1288-93",false);
    	button3=new JRadioButton("C. 1290-96",false);
    	button4=new JRadioButton("D. 1295-1301",false);
    	//array assigning
    	question1=new JLabel("1. Rivers Tigris and Euphrates are associated with?");
    	question2=new JLabel("2. Which is considered as oldest civilization of the world?");
    	question3=new JLabel("3. Which among following is called \"Gift of the Nile\"?");
    	question4=new JLabel("4. Who is considered as the master of Greek comedy?");
    	question5=new JLabel("5. When ancient Olympic games first held?");
    	question6=new JLabel("6. Who is known as the father of Modern Medicine?");
    	question7=new JLabel("7. Rome was founded around?");
    	question8=new JLabel("8. Which was not an Roman Philosopher?");
    	question9=new JLabel("9. Who was among the famous Roman poets?");
    	question10=new JLabel("10. Marco Polo, Venetian traveller, travelled from Venice to China and Japan in:");
    	button1.setBackground(Color.MAGENTA);
    	button1.setBackground(Color.BLUE);
    	button1.setForeground(Color.WHITE);
    	button1.setForeground(Color.YELLOW);
    	primary.setBackground(Color.CYAN);
    	primary.add(question1);
    	primary.add(question2);
    	primary.add(question3);
    	primary.add(question4);
    	primary.add(question5);
    	primary.add(question6);
    	primary.add(question7);
    	primary.add(question8);
    	primary.add(question9);
    	primary.add(question10);
    	//primary.add(button1);
    	//primary.add(button2);
    	//primary.add(button3);
    	//primary.add(button4);
    	
    	QuestionListener1 listener1 = new QuestionListener1();
    	button1.addActionListener(listener1);
    	button2.addActionListener(listener1);
    }
    public JPanel get_panel(){
    	return primary;
    }
    public void reset(JFrame frame){
    	question1.setText("Which move is better?");
    	button1.setSelected(false);
    	button2.setSelected(false);
    	
    	frame.setVisible(true);
    }
    public void array1(){
    }
    
    private class QuestionListener1  implements ActionListener 
    	{
    	public void actionPerformed(ActionEvent event){
    		
    		if(event.getSource()==button1){
    			question1.setText("I love vanilla too");
    		}
    		if(event.getSource()==button2){
    			question1.setText("Don't get chocolate on your shirt");
    		}
    	}
    }
    
    
}

但你是否已经考虑过使用一组数组?也许这将是一种更清洁的方式来实现同样的目标。

答案 4 :(得分:1)

您始终可以使用窗口对象访问变量。请使用以下代码动态访问您的变量。

for (var i=0, j=0; i < 4; i++) {
    console.log(window["bugNames"+i][j]);

}