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

发布于 2024-10-17 19:29:44 字数 453 浏览 2 评论 0原文

我在作业中给出的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2024-10-24 19:29:44

您的 JavaScript 代码段无效,有些东西让我认为存在复制粘贴错误。答案会根据代码的实际情况而发生很大的变化。

如果它看起来像这样:

myObj = [

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

...那么您正在处理一个对象数组,其中每个对象都有属性 namelocation。您可以使用带有索引变量的标准 for 循环来循环它们,从 0 计数到 myObj.length - 1(含),并且通过 myObj[index].namemyObj[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:

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文