1

I'm looking to pass a string into a function and return the json object of that string name.

Objname is a string, such as "ky1"

function myFunction(objname) {
    return myjsonobj.objname;
}

How can I get the above to parse correctly? This is a simplified example, but demonstrates what I'm trying to achieve.

I'm not using jQuery, this is not an option for me in this piece of work.

Any help would be great, thanks!

4

2 Answers 2

2

try with

return myjsonobj[objname];

if you write that with dot notation you're looking for a key named exactly "objname" but in your example it is only a variable.

Sign up to request clarification or add additional context in comments.

Comments

0

I think this is what you are going for. You can index the property with square brackets and it is the same thing as the period notation.

myjsonobj["ky1"] == myjosnobj.ky1


function myFunction(objname) {
    return myjsonobj[objname];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.