Java script how to construct an object using variables
Java script how to construct an object using variables
I'm working on express.js framework and sequlize.js orm.
I get the region and the type from the back-end.
Using those two variables I want to construct an object to pass to the sequlize orm.
let region_id = req.swagger.params.region_id.value;
let type = req.swagger.params.type.value;
console.log("REGION", region_id) // will output 4,5,6,... etc
console.log("TYPE", type) // will output is_lunch,is_dinner,... etc
That was my variables.
This is my object
const prepared_query =
where:
,
attributes: ['price'],
include: [
model: db.restaurants,
attributes: ['region_id'],
where:
region_id: ''
]
prepared_query.include[0].where.region_id = region_id;
prepared_query.where[type] = true;
prepared_query.include[0].where.region_id = region_id;
successfully replaced the region_id as 4. but
prepared_query.where[type] = true;
is not gives me is_lunch:true'
instead it gives me Cannot set property 'is_lunch' of undefined
is_lunch:true'
Cannot set property 'is_lunch' of undefined
HOW DO I ACHIEVE THIS USING JAVA SCRIPT?
@NisargShah I don't think so.
– Pathum Samararathna
Sep 5 '18 at 9:25
You should try doing
prepared_query[type] = true;
Let me know if that doesn't work.– Nisarg Shah
Sep 5 '18 at 9:25
prepared_query[type] = true;
I update my question
– Pathum Samararathna
Sep 5 '18 at 9:47
@NisargShah Thanks a lot. I din't heard this thing before =)
– Pathum Samararathna
Sep 5 '18 at 10:10
2 Answers
2
you can do this.
prepared_query.include[0].where.region_id = region_id;
prepared_query[type] = true;
object.property = value;
//static
"property": value;
object[property] = value;
//dynamic
[property]: value;
read this page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors
I update my question.
– Pathum Samararathna
Sep 5 '18 at 9:48
Thanks a lot. I din't heard this thing before =)
– Pathum Samararathna
Sep 5 '18 at 10:10
@PathumSamararathna this is async problem, so you use
await - async
async ajaxfunction() .. res = await getResponse()– seunggabi
Sep 5 '18 at 10:15
await - async
Now it's worked as expected!
– Pathum Samararathna
Sep 5 '18 at 10:18
I didn't set anything to the prepared_query.where
was the problem here.
prepared_query.where
prepared_query.where = ;
prepared_query.where[type] = true
Gives me the correct output.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of Dynamically access object property using variable
– Nisarg Shah
Sep 5 '18 at 9:20