对象变量的属性如何定位它们
我在作业中给出的是 JS 对象,看起来像:
myObj =
{name:eric, location:belgium, age:24},
{name:jools, location:holland, age26},
{name:mike, location:usa, age:30},
这个想法是,如果我需要以“位置”荷兰为目标,我需要能够像数组一样对待所有这些,这样我就可以使用索引(在至少我是这么认为的)。我在任何地方都找不到任何使用此方法的示例,一直在“js 对象”上进行搜索。
实际的挑战是能够通过循环将新选项元素的“名称”属性的不同值作为innerHTML(或执行类似操作的某种方法)放入给定的选择元素内。因为这是家庭作业,所以我不需要实际的代码,但是如果有一个线索告诉我可以在哪里了解更多关于这些 JS 对象属性数组类型的工作原理,那就太好了。
多谢!
What I'm given in my homework is and JS object that looks like:
myObj =
{name:eric, location:belgium, age:24},
{name:jools, location:holland, age26},
{name:mike, location:usa, age:30},
the idea is that somehow if i need to target 'location' holland i need to be able to treat all this like an arary so I can work with indexes (at least that's what I think). I Can't find any example anywhere where people work with this been searching for a bit on 'js object'.
The actual challenge is to be able to put the different values of the 'name' property as innerHTML(or some method that does something similar) of new option elements inside a given select element probably through a loop. Since this is homework, I don't need the actual code for that but a clue on where I can learn more about how these JS object property array type of things work would be nice.
thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 JavaScript 代码段无效,有些东西让我认为存在复制粘贴错误。答案会根据代码的实际情况而发生很大的变化。
如果它看起来像这样:
...那么您正在处理一个对象数组,其中每个对象都有属性
name
和location
。您可以使用带有索引变量的标准for
循环来循环它们,从0
计数到myObj.length - 1
(含),并且通过myObj[index].name
和myObj[index].location
访问每个对象的属性。Your JavaScript snippet is invalid, something makes me think there was a copy-and-paste error. The answer changes significantly depending on what the code actually looks like.
If it looks like this:
...then you're dealing with an array of objects, where each object has the properties
name
andlocation
. You can loop through them using a standardfor
loop with an index variable, counting from0
tomyObj.length - 1
(inclusive), and access the properties of each object viamyObj[index].name
andmyObj[index].location
.