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

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).

Related

How do I convert a string into a property in javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
Now I am sending length as a string as a function parameter.
function sample(operation){
var str="hello";
console.log(str.operation);
}
sample("length");
I am not allowed to change the way I am sending length(has to be a string).What can I do for this function to give me the expected output?
If you just want to access the property, use bracket notation for your property accessor:
console.log(str[operation])

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

Using the value of a string variable as object parameter accessor [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I am attempting to dynamically access an object with a property value that is contained within a string. Example below:
var toolState = {
draw_point: false;
draw_line: false;
}
var dynamicText = "draw_point";
toolState.dynamicText = true; //here is the problem
I'm fairly new to JS. Sorry if this is a silly question.
Thanks
Use bracket notation instead of dot notation for variable names as properties.
toolState[dynamicText] = true;

JavaScript Calling Object Syntax [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I need to know why this isn't working. My Javascript code accesses a variable in an object. But it appears not to be working, partly because I can't figure out the syntax.
var obj = {
size:"small",
big:false,
thing:true
}
alert(obj[size]);
I'm just not sure if I got the syntax right…
This will work here.
obj.size //returns small
OR
obj["size"] //returns small
OR
var my_var = "size"
obj[my_var] //returns small
You can reference object values either by:
obj["size"]
or
obj.size
However, there is an exception. For instance, if you have following object with a number key: (Note: key is still a string even if it's defined this way):
var obj = {
1: true
};
You can retrieve it's value only by using: obj["1"]
Hence, using obj.1 will cause a syntax error.
Therefore, your code works if you change it to e.g.: alert(obj["size"]); but I prefer to use console.log(obj["size"]); for debugging. At least, if you are playing with node.js as your tags indicates.
Cheers.

How to get the value of a property in javascript which has a dot in it's name? [duplicate]

This question already has answers here:
How to get objects value if its name contains dots?
(4 answers)
How can I access object properties containing special characters?
(2 answers)
Closed 9 years ago.
I have a property with name 'pp.phaseName' in an object 'Config'
Whenever I try to access it like Config.pp.phaseName, it's throwing error.
I've tried using Config.(pp.phaseName), Config."pp.phaseName" etc. but none was working.
Please help me on how to do this.
You have to use the square bracket notation.
Config["pp.phaseName"]

Categories