Javascript - Set array member using variable [duplicate] - javascript

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] }
);

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.

How to access object data using variable value as element in vue js [duplicate]

This question already has answers here:
Access object child properties using a dot notation string [duplicate]
(13 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 months ago.
How i can access the object data using variable value as key?
Let say i have this object:
let item = {
name: {
first_name: "Mark",
last_name: "James"
}
}
How i can get the value of first name using variable value?
I know i can access it using
item.name.first_name
What if i want to access it using variable value like this:
let key = "name.first_name"
console.log(item[key])

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

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

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

Categories

Resources