Jason
Jason

Reputation: 133

Using a variable as a reference level in an object

I'm quite new to coding html/js. I checked answers on here but either can't understand them well enough or they aren't quite what I'm looking for.

The problem is straight forward enough; if I have the following object;

var gadget = {"1":{"id":A, "name":"Rawr"},"2":{"id":B, "name":"GoGoGadget"}"1":{"id":C, "name":"Extendable Arms!"}};

Now, if I wanted to use a forloop (for whatever reason) to extract the names of these objects I would like to try;

var i = 0;
var names = [];
for (i = 0; i < gadget.length; i++) {
   names.push(gadget.i.name);
}

I'm not surprised that this doesn't work as the "i" would probably got interpreted as the string "i" here rather than it's numeral. Unfortunately I've tried a few variants that I've found online (like using names.push(gadget[i].name) which also shouldn't work since that suggests gadget is a vector and not an object) but haven't been able to figure out how to get it to work.

Is there somewhere I can find this syntax? Or is this one of those things that seems like it should be easy but js just doesn't really have a nice solution for?

Thanks!

edit:

I didn't mean to suggest I always wanted every entry in the vector, so to clarify my question further, what if I wanted to use a forloop to only find the names of gadget entries given in another object; ie given;

var searchvec = [{"id":1,"count":17},{"id":3,"count":12}];
var i = 0;
var names = [];
for (i = 0; i < searchvec.length; i++) {
   index = searchvec.i.id;
   names.push(gadget.index.name);
}

Upvotes: 1

Views: 58

Answers (3)

Martin Meeser
Martin Meeser

Reputation: 2956

I think this is what you are looking for:

   var gadget = {"1": {"id": "A", "name": "Rawr"}, "2": {"id": "B", "name": "GoGoGadget"}, "3": {"id": "C", "name": "Extendable Arms!"}};

    let names = [];
    for (let i in gadget) {
        // i is String
        names.push(gadget[i].name);
    }


    for (let i in names) {
         // i is int value
         console.log(names[i]);
    }

Using the for (let x IN o) js takes care for you no matter wether it is an object or an array. With array, the for loop will cycle through the the array indexes, with x being an int value, and with objects it will cycle through the properties of the object, with x being the property name.

See reference: MDN

Upvotes: 1

Amit Kumar Agrawal
Amit Kumar Agrawal

Reputation: 31

You can do something like below:

var names = []; 
for (let [key,value] of Object.entries(gadgets)){
    names.push(value)
}

Upvotes: 0

ilmirons
ilmirons

Reputation: 664

Javascript is a really quirky language and can cause you serious hitches if you are used to more rigorous language. In js an array is actually an object with numbers as keys + some other goodies (such as a push() method) (to see it you may try in a console: typeof []))

P.S. this means you can actually access object properties with obj[key] being key a variable, even null-valued.

Upvotes: 0

Related Questions