How to use variable as fieldkey in for loop

I have columns with the field key of problem1, problem2, problem3, etc. Is there a way I can get the value from these keys by using them as a variable in a for loop? The result from the below code is “undefined”.

for ( var i = 1 ;i<=4;i++) {
wixData . query ( “ProblemOrder” )
. ascending ( “title” )
. contains ( “account” , email ) //only one row remains
. find ()
. then ( ( results ) => {
let items = results . items
let firstItem = items [ 0 ]
console . log ( firstItem . problem+i ) //cycle through each problem and display value
})}

I’m not sure I got what you’re trying to do.
But maybe this one?:

let problem1, problem2, problem3;
wixData.query("ProblemOrder")
.ascending("title").contains("account",email)
.limit(1)
find()
.then(res => {
{problem1, problem2, problem3}  = res.items[0];
})

I am trying to get data from my collection without having to enter the specific key. In this case the keys all start with “problem” followed by a number. I am searching for a method that allows me to get the value from the dataset for key “problemi” where “i” is the current loop. I am trying to avoid needing to list all of the keys in a variable. I ended up doing that in the meantime, but am looking for a more clean solution.

Alternatively I am have also tried to get all of the keys from the dataset and cycle through those in a for loop, but I have not been able to figure out that.
In this case if my JSON is

{"problem1":"A1","problem2":"A2","problem3":"A3}

in a collection called “ProblemOrder”. Is there a way I can pull out only the keys (“problem#”) and cycle through their value one by one?

You can do something like:

let problems = [];
wixData.query("ProblemOrder")
.ascending("title").contains("account",email)
.limit(1)find()
.then(res=>{
const item = res.items[0];
let problemKeys = Object.keys(item).filter(e => e.startsWith("problem));
for (let i = 0; i < problemKeys .length; i++){
problems.push(item[problemKeys[i]]);
}
})
//you'll get an array ("problems") with the values of "problem1", "problem2" etc ordered)..