0

I have a class "Imgs" with many different properties. And i have a variable with a string which is the name of 1 property. Now i want to make a loop for an array (pictures) which consists of many "Imgs" and want to use the property from the string.

This shows what i want, but it does not work of course ;/

Edit: Of course there ist a .xyz property for "Imgs" and many others but that doesnt matter for this example.

var property = "xyz";

for (i in pictures) {
    if (pictures[i].property) { . . . }
}

greetings, I appreciate your help

easyX
  • 73
  • 1
  • 1
  • 6

3 Answers3

0

You'll want to use the index notation

var property = "xyz";

for (i in pictures) {
    if (pictures[i][property]) { . . . }
}

or

for (i in pictures) {
    if (pictures[i]['xyz']) { . . . }
}
phuzi
  • 12,078
  • 3
  • 26
  • 50
0

Try bracket notation:

var property = "xyz";
for (i in pictures) {
    if (pictures[i][property]) { . . . }
}

Bracket notation allows you to access a property by string containing its name.

It's the same you were using to iterate pictures: pictures[i].

Oriol
  • 274,082
  • 63
  • 437
  • 513
0

You can use bracket notation:

if (pictures[i][property]) { ... }
JustcallmeDrago
  • 1,885
  • 1
  • 13
  • 23