Resolve function parameters

Got a simple object (demo purposes only) and want to return values using a function but having major issues getting it to work. Here is the code and error I get:

const obj = {
“Alfred”:{“sex”:“M”, “weight”:112.5},
“Alice”:{“sex”:“F”, “weight”:84},
“Barbara”:{“sex”:“F”, “weight”:98}
};
function getData(key, name) {
const retval = obj[key].[name];
return retval;
};

getData(“Barbara”, “weight”)

Uncaught SyntaxError: Unexpected token ‘[’

How can I get this to work?

there is no need of a dot here

Thanks so much for the reply. Can’t tell you how much time I wasted trying to figure it out…

1 Like

I see that instead of even having to create a function I can get the same results doing this:

obj['Barbara'].weight  // 98
obj.Barbara.weight      //  98

What is the correct type of object here - is it a hash?

Not sure what you mean?

It’s just an object of objects. Objects are a collection of properties and properties are key/value pairs. Object keys are strings. You can call it a hash map/table/whatever, but it is still just an object in JS.

There is also Map and Set (and the Weak versions).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.