0

I'm setting up an API for a web app I am making to communicate with a database and need to support multiple tables.

I have a JSON object, well table of JSON objects ([{"name":"video_games", "columns":["name", "console", "wiki"]}, {"name":"game_wishlist", "columns":["name", "console", "wiki"]}]), that has the names of the tables and the columns, but I need a way to hit another JSON object to get specific data using the specifications in this object.

Example: games[i] = [table[i].name, table[i].console, table[i].wiki] is what I currently have but I need a way where I can set the name, console, and wiki to something else for other tables from the object I showed above without just setting up a ton of if statements and hard coding it.

2 Answers 2

1

If I'm reading this right, then you should be aware that you can access object properties using similar bracket notation to arrays, letting you use a variable string for the key. So for instance, in your case you could do:

let keyName = 'wiki';
games[i][keyName] = table[i][keyName];

That would produce (assuming games[i] existed before) the same result as games[i].wiki = table[i].wiki', except now obviously you could always change the value of keyName dynamically to create and read different properties.

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

1 Comment

Thanks, I figured it out on my own, I didn't even know bracket notation for objects was a thing till now but this really helps.
0

I think I figured it out, I can't test it here, don't have access to the db server right now but I should be able to use bracket notation (something I just learned) to do it. https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics#Bracket_notation

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.