Home Forums Tech Web Development JS: How to access object property with variable

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #4289
    Mr. Mangus
    Participant

    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.

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.