displaying multiple arrays using javascript

时间:2017-04-10 03:13:55

标签: javascript arrays

these are my arrays

var CarType=["RM","BM","GM"];
var CarName=["Red Mustang","Black Mustang","Green Mustang"]

how would I create a simple function that when a button is pushed an alert would pop up that displays something like this:

RM = Red Mustang

BM = Black Mustang

GM = Green Mustang

2 个答案:

答案 0 :(得分:1)

Here's a working solution. Hope it helps!

function myFunction() {
   var CarType=["RM","BM","GM"];
   var CarName=["Red Mustang","Black Mustang","Green Mustang"];
   var result = "";
   for(var i in CarType){
     result += [CarType[i] + " = " + CarName[i]] + "\n";
    }
   alert(result)
}
<button onclick="myFunction()">Click me</button>

答案 1 :(得分:0)

我没有测试过这个,但它应该是这样的。

您的数组必须具有相同的大小才能工作和排序

function display(carType, carName) {

     var text = "";

     for (i=0; i < carType.length; i++) {
         text += carType[i] + " = " + carName[i] + "\n";
     }

     alert(text);
}

var CarType=["RM","BM","GM"];
var CarName=["Red Mustang","Black Mustang","Green Mustang"];

display(CarType, CarName);