0

I have a list of items that I would like to iterate through, adding the value of i to each to find the next set of information.

Example: You can concatenate the string "req.body.item" + i + "Title" to get the result "req.body.item0Title"

But can you do that with the object req.body.item + i + Title? I keep getting "Title isn't defined or something similar.

for (i = 0; i < 3; i++) {
    console.log(req.body.item + i + Title);
}

Ideally, I'd love to see the console.log output to read the req.body reference of, item0Title, item1Title, and item2Title.

Thanks team.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
Newsworthy
  • 39
  • 13
  • What exactly is `item`? Is it an array? – Brad Jan 22 '19 at 21:38
  • 2
    `req.body[item + i + Title]` – Kevin B Jan 22 '19 at 21:38
  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Kevin B Jan 22 '19 at 21:39
  • Currently, no. It's from a form with user input with three inputs. I'd like them to be in an array on completion of those three, and then inserted into another array with another twenty, but working through this in stages, since my last question became a wall of code and was rightfully ignored. – Newsworthy Jan 22 '19 at 21:40
  • Kevin B - I get ReferenceError "Title is not defined". – Newsworthy Jan 22 '19 at 21:42
  • Then why are you trying to add it to `i`? did you intend for it to be a string? – Kevin B Jan 22 '19 at 21:46
  • @Newsworthy put it in quotes, like this - `req.body[item + i + "Title"]` – Al.G. Jan 22 '19 at 21:48
  • I have a series of form inputs that follow the format of item1Title, item2Title, item3Title etc... and I can get the console.log of them with req.body.item1Title. I'm looking to iterate through a group of them, in this case with the i = 1-3 loop, without having to write it out three times. Is this possible? – Newsworthy Jan 22 '19 at 21:49
  • Try `console.log(req.body.item + i + 'Title');` if you want `Title` as a string, although i don't understand what you want to do. – dimitris tseggenes Jan 22 '19 at 21:50
  • @dimitristseggenes OP wants to get the `item0Title` property of `req.body` without manually listing `item1Title` etc by hand. So they need what Kevin suggested, though he forgot the quotes around 'Title'. – Al.G. Jan 22 '19 at 21:54
  • @dimitristseggenes - I get NaNTitle on output three times. – Newsworthy Jan 22 '19 at 21:55
  • @Al.G. - YES! That did it, apologies I was so confusing to everyone. I got it with console.log(req.body["item" + i + "Title"]); Thanks dimitristseggenes and Kevin B as well. Appreciate your time. – Newsworthy Jan 22 '19 at 21:55

1 Answers1

1

This is the cleanest way to do what you need.

for (let i = 0; i < 3; i++) {
    console.log(req.body[`item${i}Title`]);
}

¿How does it work?

Objects are sometimes called associative arrays, since each property is associated with a string value that can be used to access it.

ccordon
  • 1,072
  • 12
  • 22
  • That worked! Thank you, very helpful indeed. May I ask the significance of the $ sign? Does it indicate something unusual is coming next? – Newsworthy Jan 23 '19 at 00:24
  • Single quotes (') and Double quotes (") they are used when working with strings, grave accent (\`) represent Template Strings, Template Strings can contain placeholders. These are indicated by the dollar sign and curly braces `(${expression})`. The expressions in the placeholders and the text between them get passed to a function. The default function just concatenates the parts into a single string, in this case we use it to be able to refer to the value of a variable and at the same time concatenate the string. – ccordon Jan 23 '19 at 04:49
  • Thanks for the extra explanation @ccordon. Appreciated. – Newsworthy Jan 23 '19 at 08:39