Add a variable to a response path [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
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);
})
};

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]

Related

How to get info from json using a variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
So I'm trying to get data from a JSON and I declared a variable and want to use the thing from that variable to get info from the JSON I'm trying to do this but I don't know what the problem is
let e = "KA8AL9AE";
let codes = '{"T9R3P5YE": "10", "8HAW69VC": "20"}'
codes = JSON.parse(codes)
console.log(codes.e)
I've tried doing
let e = "KA8AL9AE";
let codes = '{"T9R3P5YE": "10", "8HAW69VC": "20"}'
codes = JSON.parse(codes)
console.log(`${codes. + e}`)
But it didn't work, Is there a way to do this?
You could call the new key like:
codes[e]

Local variable not being recognized inside for loop - Javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 years ago.
I'm trying to reference a variable inside this for loop, but its not working. It recognizes the first variable in the line but anything beyond that it won't recognize
var farmsArray = Object.keys(farmsPath);
var farmsList = new MessageEmbed()
.setTitle("Available Farms")
.setDescription("Where would you like to plant?")
.setColor(0xFFF00);
for(i=0; farmsArray[i] != undefined; i++){
console.log(i);
farmsList.addField(` ${farmsPath.farmsArray[i].name}`, farmsPath.farmsArray[i].size);
it recognizes this^ ^ but not this
};
Can someone help me find out why? I've been trying for a while now and I can't seem to figure out the issue.
Based on var farmsArray = Object.keys(farmsPath); I'll imagine farmsPath is an object whose keys you want to iterate over.
var farmsList = new MessageEmbed()
.setTitle("Available Farms")
.setDescription("Where would you like to plant?")
.setColor(0xfff00);
Object.keys(farmsPath).forEach((key) => {
farmsList.addField(
` ${farmsPath[key].name}`,
farmsPath[key].size,
);
});
could be closer to what you're looking for.

Using string as variable name in JS? [duplicate]

This question already has answers here:
Is there a way to access a javascript variable using a string that contains the name of the variable?
(4 answers)
How to use a string as a variable name in Javascript? [duplicate]
(3 answers)
parse a string as an object from data attribute [duplicate]
(1 answer)
Use dynamic variable names in JavaScript
(19 answers)
Closed 9 years ago.
How can I make the code that follows to work?
var x = 'name';
and then, to use the value inside x, as it was a variable, and set it, so that if i want it to be NAME, i'll have the result:
var name = 'NAME';
Is it possible ?
Not directly, no. But, you can assign to window, which assigns it as a globally accessible variable :
var name = 'abc';
window[name] = 'something';
alert(abc);
However, a much better solution is to use your own object to handle this:
var name = 'abc';
var my_object = {};
my_object[name] = 'something';
alert(my_object[name]);
I haven't seen the rest of your code, but a better way might be to use an object.
var data = {foo: "bar"};
var x = "foo";
data[x]; //=> bar
var y = "hello";
data[y] = "panda";
data["hello"]; //=> panda
I think this is a little cleaner and self-contained than the window approach

Updating a result in JSON with a dynamic variable name [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I need to update a value in my JSON.
My JSON result looks like this:
results = {"ROWCOUNT":50,"COLUMNS":["PERSONID","NAME"],"DATA":{"PERSONID":["42","43","44"], "NAME":["JOE","TOM","JANE"]}
resultData = results.DATA
In the below code I am looping over the result set and attempting to update a value at a position. I believe it is failing because I am not using dynamic variables correctly.
var columnName = "NAME";
for(i=0; i < results.ROWCOUNT; i++ ){
resultData.columnName[i] = "foo" // failing here due to "columnName" being dynamic.
}
Figured it out.. You have to use array syntax
resultData[columName][i]

Define Javascript object using dynamic string literal as object propery name [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 3 years ago.
Needing to do:
var some = {
`${foo1}_${foo2}`: bar
}
but this gives a syntax error though I must do it somehow. How?
you can suppose object as hashmap and access properties via []
var foo1 = 'a';
var foo2 = 'b';
var some = {};
some[foo1+'_'+foo2] = 'test';
console.log(some.a_b);

Categories