简体   繁体   English

我正在尝试创建一个下拉菜单,用于存储餐馆菜单中的项目,使用vanilla JavaScript传递一组对象

[英]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.

相关问题 从Flask传递数组到Javascript以创建下拉菜单选项 - Passing array from Flask to Javascript to create options for drop down menu Vanilla Javascript 切换下拉菜单 - Vanilla Javascript Toggle Drop Down Menu html、css 和 javascript 的新手。 我无法从 onclick 的下拉菜单中将变量传递给我的 javascript 函数 - New to html,css, and javascript. I am having trouble passing variables to my javascript function from a drop down menu onclick 我正在使用Push()数组试图从下拉菜单中选择一个列表并将该列表发布到一个范围中吗? - Am using Push() array was used am trying to use for select a list from drop down menu and post that list into a span? 我正在创建javascript下拉菜单,但无法正常工作? - i am creating javascript drop down menu but its not working as expected? 在下拉菜单中从数组中带回特定项目 - Bringing specific items back from an array in a drop-down menu 试图使用javascript制作下拉菜单,但它不起作用? - Trying to make a drop down menu using javascript and it is not working? I'm trying to source values for a select drop down from a table using JavaScript and jQuery, but the select menu doesn't show any values? - I'm trying to source values for a select drop down from a table using JavaScript and jQuery, but the select menu doesn't show any values? 我正在尝试使用 Vanilla JavaScript 从 TODO-LIST 应用程序中删除项目 - I am trying to delete Items from in a TODO-LIST App using Vanilla JavaScript 因此,我试图使用for循环从数组中获取一组项目并将其显示到上下文菜单子菜单中,但是以某种方式,我无法 - So I am trying to get a set of items from an array using a for loop and displaying them into a context menu sub menu, but somehow I am not able to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM