简体   繁体   English

对象变量的属性如何定位它们

[英]properties of object variables how to target them

What I'm given in my homework is and JS object that looks like: 我在作业中得到的是和JS对象类似的内容:

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). 我的想法是,如果我需要定位到“位置”荷兰,我就必须能够像对待arary一样对待所有这些东西,以便可以使用索引(至少我是这样认为的)。 I Can't find any example anywhere where people work with this been searching for a bit on 'js object'. 我在人们与之合作的任何地方都找不到“ js对象”的任何示例。

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. 实际的挑战是要能够通过循环将给定的select元素中的新的option元素放入不同的name属性值作为innerHTML(或执行类似操作的方法)。 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. 由于这是家庭作业,因此我不需要实际的代码,但是可以从哪里获得更多有关这些JS对象属性数组类型如何工作的线索。

thanks a lot! 非常感谢!

Your JavaScript snippet is invalid, something makes me think there was a copy-and-paste error. 您的JavaScript代码段无效,这使我认为存在复制和粘贴错误。 The answer changes significantly depending on what the code actually looks like. 根据代码的实际情况 ,答案将发生重大变化。

If it looks like this: 如果看起来像这样:

myObj = [

    {name:eric, location:belgium, age:24},
    {name:jools, location:holland, age26},
    {name:mike, location:usa, age:30},
    // ...
    ];

...then you're dealing with an array of objects, where each object has the properties name and location . ...然后您要处理一个对象数组,其中每个对象都具有属性namelocation You can loop through them using a standard for loop with an index variable, counting from 0 to myObj.length - 1 (inclusive), and access the properties of each object via myObj[index].name and myObj[index].location . 您可以使用带有索引变量(从0myObj.length - 1 (含))的标准for循环遍历它们,并通过myObj[index].namemyObj[index].location myObj.length - 1访问每个对象的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM