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

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

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.

Unable to get object data when used multiple-argument function [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I am unable to get the data of an object
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a.val[0])
}
fun('ab','ef')
It should output 'cd' but it is giving out error in the console
any idea how do i fix this...
Use bracket notation like so:
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a[val[0]])
}
fun('ab','ef')
Your code was trying to get the property named val in a (doesn't exist), then get the first character/item of that value (trying to do this to undefined causes the error).

Get object element from a defined string [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
how to get data from object with define string?
case:
var data = [
{name:"Sharma",country:"India"},
{name:"Udin",country:"Indonesia"},
{name:"John Carter",country:"Mars"}
];
getData(data,"country");
function getData(data,element){
console.log(data[1].element);
}
i want to get country but result is undefinded, how to fix this?
You would need to know both the index and the property
function getData(data,index,element){
console.log(data[index][element]);
}
getData(data,1,"country");
function getData(data,element){
console.log(data[1][element]);
}
That's the correct way to access the value by using a key that is a string.

How to use an object's value as the parameter for another object in Javascript? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 6 years ago.
If I have:
var key = "Section";
course.key = "101";
I get told course.key is unidentified. I'm trying to set course.Section = "101". Is there anyway to pass key's value in the second line?
The property key of course is undefined. Use bracket notation.
course[key] = "101"
is the same as
course["Section"] = "101"

Get value of object inside a for loop with dynamic variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I'm trying to get a value of property (or more) that user provides inside a for loop.
OBJECT:
RULES: {
"required": /.+/,
"numeric": /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/
}
FOR LOOP:
$("input").keyup(function() {
var inputVal = $(this).val();
var rules = $(this).data('rules').split(" ");
for (var i = 0; i < rules.length; i++) {
console.log(rules[i]); // OK - return "required" and "numeric"
console.log(RULES.required); // OK - return "/.+/"
console.log(RULES.rules[i]); // NOT OK
};
});
MARKUP
<input data-rules="required numeric" type="text">
The problem is that it provokes an error: "Cannot read property '0' of undefined".
So how can I look for the value of "rule[i]" and not "rule[i]" itself?
Why does not translate by itself?
Thank you in advance.
you are doing:
RULES.rules[i]
you should do:
RULES[rules[i]]
when you do the first one, Javascript first looks for a rules property on the RULES object (but this property doesn't exist) then it tries to access element 0 of the rules property, which doesn't exist... hence you get "Cannot read property '0' of undefined" error
in the case where you want to access the property of an object using a variable for the name you cannot use dot notation any more and must use the square brackets

Categories