1

I'm trying to add a variable into a path like this:

response.response.body.result.company.data.t${variable}.month_col_val[i]

I know it's wrong, yet I'm not being able to find if it's actually posible to do it and how to do it. My json file contains objects named t0, t1, t2... and so on inside data, so i'm trying to get an specific one by adding the variable value along the T

Variable is define outside the fuction and pass as a parameter.

for (let o = 0; o < 12; o++) {
                    cy.get($el).find('.month-data-cols').eq(o).invoke('text').then($text => {
                        let monthGross = $text
                        let monthGrossReq = response.response.body.result.company.data.t${variable}.month_col_val[o]
                        monthGross = monthGross.toString().replace(/,/g, '').trim();
                        expect(monthGross).to.include(monthGrossReq);
                    })
                };
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • To all new users tempted to answer this softball question: It's been asked and answered before! Please don't answer it again! If you think you have something new to add, add it to the duplicate. – Heretic Monkey Oct 13 '21 at 20:21

1 Answers1

3

Try using square-bracket notation instead of dot-notation at that property

const data = response.response.body.result.company.data
const monthGrossReq = data[`t${variable}`].month_col_val[o]
Fody
  • 23,754
  • 3
  • 20
  • 37