开发者

properties of object variables how to target them

开发者 https://www.devze.com 2023-02-10 14:14 出处:网络
What I\'m given in my homework is and JS object that looks like: myObj = {name:eric, location:belgium, age:24},

What I'm given in my homework is and JS object that looks like:

myObj = 

    {name:eric, location:belgium, age:24},
    {name:jools, location:hol开发者_开发技巧land, 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消