[英]I am trying to create a drop down menu which stores items from a restaurant menu, using vanilla JavaScript by passing it an array of objects
I am getting no errors but when I click the drop down menu I want it to display the name of the item and the price. 我没有错误,但当我点击下拉菜单时,我希望它显示项目的名称和价格。
Instead it just displays [object Object]. 相反,它只显示[object Object]。
I have tried appending itemList.item[i] but get the error TypeError: Cannot read property '0' of undefined
我试过附加itemList.item [i]但得到错误
TypeError: Cannot read property '0' of undefined
/*
///Food Items
*/
/*Constructor which stores the items on the menu*/
function Item (item, price) {
this.item = item;
this.price = price;
}
var iOne = new Item('Peene Primviera', 14.60);
var iTwo = new Item('Fillet Steak', 20.00);
var iThree = new Item('Fillet Steak', 20.00);
var foodSelection = document.getElementById('menu');
var itemList = [
{iOne},
{iTwo},
{iThree}
];
var itemName = itemList.name;
//Add the Options to the DropDownList.
for (var i = 0; i <= itemList.length; i++) {
foodSelection.options.add(new Option(itemList[i]));
};
The result I want is just to display the name of the item and the price of the item in the drop down list 我想要的结果只是在下拉列表中显示项目的名称和项目的价格
the [Object object]
is there because you convert an object
to a string
which is done by calling Object.toString()
returning [Object object]
by defaul [Object object]
是因为你将一个object
转换为一个string
,这是通过调用Object.toString()
通过defaul返回[Object object]
来完成的
There are some problems in your code, see the comment in the snippets 您的代码中存在一些问题,请参阅代码段中的注释
/* ///Food Items */ /*Constructor which stores the items on the menu*/ function Item (item, price) { this.item = item; this.price = price; } var iOne = new Item('Peene Primviera', 14.60); var iTwo = new Item('Fillet Steak', 20.00); var iThree = new Item('Fillet Steak', 20.00); var foodSelection = document.getElementById('menu'); var itemList = [ iOne, // you don't need to surround these with {} iTwo, iThree ]; var itemName = itemList.name; for (var i = 0; i < itemList.length; i++) { // here using <= is not good because i goes to 3 while your index goes up to 2 (start at 0) var item = itemList[i] var value = item.item + " (" + item.price +")" foodSelection.options.add(new Option(value)); // by default creating a string from a object create [Object object] // so you need to create the string yourself };
<select id="menu"></select>
as mention by ths
in comments, replacing 如提及
ths
的意见,更换
for (var i = 0; i < itemList.length; i++) {...}
by 通过
var count = itemList.length
for (var i = 0; i < count; i++) {...}
give some better performance as the second expression in for loop is recalculated every time BUT you seem to be starting with js and don't need to be preocupated by this unless you loop over thousands of elements 提供一些更好的性能,因为每次重新计算for循环中的第二个表达式但是你似乎是以js开头并且不需要通过它进行预先更新,除非你循环遍历数千个元素
another way to do it is to create a toString
in Item
另一种方法是在
Item
创建一个toString
/* ///Food Items */ /*Constructor which stores the items on the menu*/ function Item (item, price) { this.item = item; this.price = price; this.toString = function() { return this.item + " (" + this.price +")" } } var iOne = new Item('Peene Primviera', 14.60); var iTwo = new Item('Fillet Steak', 20.00); var iThree = new Item('Fillet Steak', 20.00); var foodSelection = document.getElementById('menu'); var itemList = [ iOne, iTwo, iThree ]; var itemName = itemList.name; for (var i = 0; i < itemList.length; i++) { var item = itemList[i] foodSelection.options.add(new Option(item)); // calling item.toString() behind the hood };
<select id="menu"></select>
ItemList[i]
will return an Item object. ItemList[i]
将返回Item对象。 You now want to display the name of that object (which I believe is the item attribute). 您现在想要显示该对象的名称(我认为是item属性)。 So you have a list of Item objects, you can use array notation to index elements inside of that.
因此,您有一个Item对象列表,您可以使用数组表示法来索引其中的元素。 However trying to index
ItemList.item[i]
would not make any sense, unless ItemList was an object containing and item attribute that was an array. 然而,尝试索引
ItemList.item[i]
没有任何意义,除非ItemList是包含和作为数组的item属性的对象。 I would try this line: 我会尝试这一行:
foodSelection.options.add(new Option(itemList[i].item));
Note that this will only display the item name. 请注意,这只会显示项目名称。 Now you need to append the price onto it.
现在你需要将价格附加到它上面。
foodSelection.options.add(new Option(itemList[i].item +" " +itemList[i].price));
You also will need to make a change to how you are adding Objects to your ItemList Array. 您还需要更改如何将对象添加到ItemList数组。
var itemList = [
{iOne},
{iTwo},
{iThree}
];
This is essentially saying you want an array of objects that contain an Item object inside of them. 这基本上是说你想要一个包含Item对象的对象数组。 In other words, you do not want to surround the elements of the array with
{}
the object notation indicators. 换句话说,您不希望用
{}
对象表示法指示符包围数组的元素。
In stead, populate your array like so: 相反,填充您的数组如下:
var itemList = [
iOne,
iTwo,
iThree
];
The main issue is surrounding the instances of the Item
class into {
and }
( {iOne}
), this actually will create a new object that has one attribute named the same as the variable name ( {iOne}
=> iOne
is the attribute's name) thus there's no attribute called 0
. 主要问题是将
Item
类的实例包含在{
和}
( {iOne}
)中,这实际上将创建一个新对象,其中一个属性与变量名称相同( {iOne}
=> iOne
是属性的名称因此没有名为0
的属性。
In order to bypass that, you'll simply populate the itemList
array without the curly braces like so : 为了绕过它,你只需填充
itemList
数组而不用如下的花括号:
var itemList = [
iOne,
iTwo,
iThree
];
And now you can iterate through that array as usuall : 现在,您可以通过以下方式遍历该数组:
/*Constructor which stores the items on the menu*/ function Item(item, price) { this.item = item; this.price = price; } const iOne = new Item('Peene Primviera', 14.60), iTwo = new Item('Fillet Steak', 20.00), iThree = new Item('Fillet Steak', 20.00), foodSelection = document.getElementById('menu'), itemList = [ iOne, iTwo, iThree ]; itemList.forEach(i => { const o = document.createElement('option'); o.value = i.price; o.textContent = `${i.item} ${i.price}$`; foodSelection.options.add(o); });
<select id="menu"></select>
new Option
expects strings and you are passing in an object. new Option
需要字符串并且您正在传入一个对象。 Reference the text 参考文字
foodSelection.options.add(new Option(itemList[i].item));
and if you want the value 如果你想要这个价值
var opt = itemList[i]
foodSelection.options.add(new Option(opt.item, opt.price));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.