Home > Blockchain >  how to do a for loop on fixtures in cypress - array with variable
how to do a for loop on fixtures in cypress - array with variable

Time:05-25

the fixtures:

{
  "new_application" : 1,
  "diagnosis_application" : 2,
  "details_completion_application" : 3,
  "awaiting_treatment_application" : 5,
  "closed_application" : 6,
  "canceled_application" : 7

}

I need the variables names

CodePudding user response:

You can use Object.keys() to get all the keys from the JSON object.

var jsonObj = {
  "new_application" : 1,
  "diagnosis_application" : 2,
  "details_completion_application" : 3,
  "awaiting_treatment_application" : 5,
  "closed_application" : 6,
  "canceled_application" : 7

}
Object.keys(jsonObj)

Object.keys(jsonObj) will return an array with all the keys:

[
  'new_application',
  'diagnosis_application',
  'details_completion_application',
  'awaiting_treatment_application',
  'closed_application',
  'canceled_application',
]

To get the keys one by one you can use the for-Each loop like this:

Object.keys(jsonObj).forEach(function(key) {
    var value = jsonObj[key]; //Gets each key one by one
})

CodePudding user response:

The syntax is for...in, see MDN

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// expected output:
// "a: 1"
// "b: 2"
// "c: 3"

  • Related