JavaScript object like String literal doesn't to set property [duplicate] - javascript

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 2 years ago.
In JavaScript everything is object like String literal, numbers, Object...
And properties can be added to objects dynamically also, but I am adding property to object which refer to string then it seems it is not working
Following is JS code which I am running
var aVar = "some string";
console.log(aVar);
aVar.name = 'raj';
console.log(aVar.name);
Output...
hi
some string
undefined

Related

get Object value using Object's key where key is a string [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed last month.
I have one object
my_object = {"first_name":"Harry", "age":"24", "second_name":"snow"};
I can get the first name value using
console.log(my_object.first_name); //Harry
and I am getting Harry . Here everything working fine . But for the below code I am not getting values
var num_1 = "first_name";
console.log(my_object.num_1); //undefined
console.log(my_object+"."+num_1); //[object Object].first_name
Please help to solve this. Why I am not able to get object values using num_1 . I need to get the object values using num_1.

Javascript - Set array member using variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 5 years ago.
I need to be able to set the member of an array using a variable, catch being that the variable defines the name of another arrays member. I'm writing in Typescript and ES6.
The code is passed an array as a variable e but the name of the text: key will be e.[variable].
What is the correct format for setting that variable member?
options.push(
{ key: e.Id, text: e.[variable] }
);

In Javascript, can I reference a key value pair from another key value pair? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Can a JavaScript object property refer to another property of the same object? [duplicate]
(2 answers)
How do I reference the same Object's properties during its creation? [duplicate]
(5 answers)
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Can I reference other properties during object declaration in JavaScript? [duplicate]
(7 answers)
Closed 5 years ago.
var fighters = ['johnjones', 'rondarousey', 'connormcgregor', 'chuckliddel', 'demetriusjohnson'];
var warriors = {
wrestlers: ['randysavage', 'hulkhogan', 'ultimatewarrior', 'jakethesnake', 'milliondollarman'],
stable: [fighters, warriors.wrestlers]
}
I believe I can reference fighters from stable, but can I reference wrestlers from stable? In other words, how do I reference a key value pair from a later key value pair within the warriors object. Thank you for any and all help!
You cannot do that until the variable warriors is initialized. The only way is to wait and assign in the next line:
var warriors = {
wrestlers: ['randysavage', 'hulkhogan', 'ultimatewarrior', 'jakethesnake', 'milliondollarman'],
}
warriors.stable = [fighters, warriors.wrestlers]
Or use some kind of weird initialization like the one described here. Or use function constructor.

How to access object property as a string in javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Access JSON or JS property using string
(4 answers)
Closed 6 years ago.
I can do var foobar = foo.barto access the bar variable.
How would I do this if I had an arbitrary string that told me what element I needed to access. foo."bar" doesn't work.
I think
foo["bar"]
is the syntax you need

how do you look at the contents of an object in javascript [duplicate]

This question already has answers here:
How do I enumerate the properties of a JavaScript object? [duplicate]
(14 answers)
Closed 8 years ago.
I am very new to javascript. When I do a typeof variable name, I get object{}. I need to look at the contents of this object. How would I do that in javascript?
You can use developer's console (Chrome console / Firebug / etc) or just convert your object to string:
var objAsString = JSON.stringify(yourVariable);

Categories

Resources