我无法想象这一点,我希望MD数组存储一本书的名称和价格。我知道这对两个数组来说更容易,但我真的想在javascript中给出一个MD数组。我尝试了几种方法,但只能显示两次书名或书价。任何帮助将不胜感激。
var bookName = new Array();
for (var i = 0; i < 3; i++)
{
bookName[i] = new Array();
for (var j = 0; j < 1; j++)
{
bookName[i] = prompt("Please enter book name!");
bookName[[i][j]] = prompt("Please enter book price!");
document.write(bookName[i] + bookName[[i][j]] + "</br>");
}
}
答案 0 :(得分:5)
我认为你应该只使用一个对象数组:
var books = [];
for(var i = 0; i < 3; i++) {
books.push({
name: prompt("Please enter book name!"),
price: prompt("Please enter book price!")
});
document.write(books[i].name + books[i].price + "</br>");
}
如果您愿意,可以改为使用数组:
var books = [];
for(var i = 0; i < 3; i++) {
books.push([prompt("Please enter book name!"), prompt("Please enter book price!")]);
document.write(books[i][0]+ books[i][1] + "</br>");
}
答案 1 :(得分:4)
以下是构建2D数组的方法:
var bookName = new Array();
for (var i = 0; i < 3; i++)
{
bookName[i] = new Array();
bookName[i][0] = prompt("Please enter book name!");
bookName[i][1] = prompt("Please enter book price!");
document.write(bookName[i] + bookName[i][0] + ": " + bookName[i][1] + "</br>");
}
您也可以使用数组中的对象执行此操作:
var bookName = new Array();
for (var i = 0; i < 3; i++)
{
bookName[i] = {};
bookName[i]["name"] = prompt("Please enter book name!");
bookName[i]["price"] = prompt("Please enter book price!");
document.write(bookName[i] + bookName[i].name + ": " + bookName[i].price + "</br>");
}
答案 2 :(得分:0)
你覆盖你的bookName [i]
bookName[i] = new Array();
和bookName[i] = prompt("Please enter book name!");
试试这个:
var bookName = new Array();
for (var i = 0; i < 3; i++) {
bookName[i] = new Array();
for (var j = 0; j < 1; j++) {
var name = prompt("Please enter book name!");
var price = prompt("Please enter book price!");
bookName[i][j] = {name:name, price:price}
document.write(bookName[i][j].name + bookName[i][j].price + "</br>");
}
}
答案 3 :(得分:0)
而不是:
bookName[i] = prompt("Please enter book name!");
bookName[[i][j]] = prompt("Please enter book price!");
document.write(bookName[i] + bookName[[i][j]] + "</br>");
这样做:
bookName[i][j][0] = prompt("Please enter book name!");
bookName[i][j][1] = prompt("Please enter book price!");
document.write(bookName[i][j][0] + bookName[i][j][1] + "</br>");
我认为我以前从未见过var [[i] [j]]格式......
答案 4 :(得分:0)
多维数组在这里没有多大意义,你可能想要一个对象数组。
var books = [], i = 3;
while( i-- ) {
books.push({
name: prompt("Please enter book name!"),
price: prompt("Please enter book price!")
});
}
答案 5 :(得分:0)
我不确定你在那里做什么。请熟悉JavaScript中的数组,对象和属性访问的概念。
var bookName = new Array();
for (var i = 0; i < 3; i++)
{
bookName[i] = new Array();
// the i. item of bookname holds an empty array now
for (var j = 0; j < 1; j++)
{
bookName[i] = prompt("Please enter book name!");
// you're overwriting the array just created with a string now
bookName[[i][j]] = prompt("Please enter book price!");
// this is absolutely invalid syntax
document.write(bookName[i] + bookName[[i][j]] + "</br>");
// as well as this
}
}
你想做什么:
var bookName = new Array();
for (var i = 0; i < 3; i++) {
bookName[i] = new Array();
for (var j = 0; j < 1; j++) {
// this is absolute ugly in a for loop:
if (j==0)
bookName[i][j] = prompt("Please enter book name!");
else if (j==1)
bookName[i][j] = prompt("Please enter book price!");
}
// direct accessing is better:
document.write(bookName[i][0] + bookName[i][1] + "</br>");
}
但是这样会容易得多:
var bookName = [];
for (var i = 0; i < 3; i++) {
bookName[i] = {
name: prompt("Please enter book name!"),
price: prompt("Please enter book price!")
};
document.write(bookName[i].name + bookName[i].price + "</br>");
}
答案 6 :(得分:0)
var bookName = [];
for (var i = 0; i < 3; i++)
{
var b = bookName[i];
b = [];
for (var j = 0; j < 1; j++)
{
bookName[i] = prompt("Please enter book name!");
b[j] = prompt("Please enter book price!");
document.write(bookName[i] + b[j] + "</br>");
}
}
应该这样做..