简体   繁体   中英

properties of object variables how to target them

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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