Why does NodeJS/Javascript insist these variables are undefined? [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
I've been grappling with this issue for about an hour now, here is the offending code:
const t = games[0];
for (const mvar in t) {
if (t.hasOwnProperty(mvar))
console.log(`${mvar}: ${t.mvar}`);
}
The output is:
appid: undefined
name: undefined
playtime_forever: undefined
img_icon_url: undefined
img_logo_url: undefined
has_community_visible_stats: undefined
However, the WebStorm debugger says this value is not undefined as such:
Are there any other reasons that this could be?

t has no property named myVar.
You want t[myVar] to get the property with that name.

Related

My JavaScript function isn't using one of the variables i need it to [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I'm creating a mongoose schema and need to do required checks a lot, so I tried creating this function. But it says that 'subcategory' is initialized but never used. How can I get the function to check this. like I need it to?
function requiredCheck(subcategory, value) {
return this.subcategory === value;
}
Use bracket accessors to check a property of this by variable:
function requiredCheck(subcategory, value) {
return this[subcategory] === value;
}
Otherwise you're checking for the property with the literal key "subcategory" which probably isn't what you want.

Deconstructing property from object throws error [duplicate]

This question already has answers here:
How to bind methods when destructuring an object in JavaScript?
(5 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 4 years ago.
This code in ES6:
var myObj = {firstname: "Stevey",
printFirstName() {
console.log(this.firstname);
}}
function printNames({printFirstName}) {
printFirstName();
}
printNames(myObj);
TypeError: Cannot read property 'firstname' of undefined. How to fix this?
Edit: This question is not a duplicate. The code in 'exact' answer marked, does not take function from deconstructed object as function param. And the other answer marked is an essay on how 'this' works. That is just a reference not the direct answer.

Call variable names (w/ number) using for loop [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
I'm trying to get values from req.body.answerX using for loop instead coding each of them.
I've stored values e.g. into "answer1, answer2" etc.
This was my attempt:
for( var i = 1; i <= 10; i++){
console.log(req.body.answer[i]);
}
That gives me following error:
TypeError: Cannot read property '1' of undefined
How can I achieve this?
Update from
console.log(req.body.answer[i]);
to
console.log(req.body["answer" + i]);
For reference, Property Accessor

What is difference between undefined and null in javascript and what are their usecases? [duplicate]

This question already has answers here:
JavaScript checking for null vs. undefined and difference between == and ===
(8 answers)
Closed 7 years ago.
Anyone please tell what is the difference between undefined and null in javascript and when I should use undefined and when null.
undefined means a variable has been declared but has not yet been assigned.
null is an assignment value. It can be assigned to a variable as a representation of no value
example.
var a;
alert(typeof(a));
var b = null;
alert(typeof(b));
Running the above script will result in the following output:
undefined
object
——————————-
undefined is not defined at all... means, that given variable does not exist.
null is defined variable that has value set to null (so basically you can say that it is defined variable with undefined value)

Adding custom variables to an object [duplicate]

This question already has answers here:
How to convert string as object's field name in javascript
(5 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
Suppose I have an object obj which has some variables in it. What I want to do is to add a new variable which has a name i gave as a parameter.
For example:
var obj=
{
number:8
}
function AddField(field_name,obj)
{
//some magical code here
}
If I call
AddField('name',obj);
i want to be able to do
obj.name='Apple'
after calling this function.
Any ideas?
Use square brackets notation:
obj[field_name] = value;
REF: http://www.jibbering.com/faq/faq_notes/square_brackets.html#vId
Use the bracket notation:
obj[field_name] = void 0;
This creates a new property with an undefined value.
If obj is already defined, you can just do obj.field_name = null or obj["field_name"] = null.
I wouldn't set any value to undefined even though the language allows for that, simply for a semantic issue: undefined means "this property/variable has not been defined" and null means "this property/variable has no value".

Categories