- This topic has 0 replies, 1 voice, and was last updated 8 years, 6 months ago by .
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
Home › Forums › Tech › Web Development › JS: How to access object property with variable
I encountered this while coding in Node.js, but this technique applies to JavaScript in general.
Usually we access response from user as
req.body.LastName
or something similar. But if the form contains multiple input fields like LastName111, LastName112, LastName113 how to access individual req.body.LastName111
if all we have is a variable with recordID = 111?
The answer is so simple and so fundamental to understanding JS that I took a few minutes to write it down:
req.body["LastName" + recordID]
In the first example we used DOT notation and here we are using BRACKET notation.
© 2017 DomainWebCenter.com. All Rights Reserved. | Disclaimer | Contact the Editor