Accessing element by variable [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have an array and want to access its data. However, I need to use a variable instead of the name to access the data.
For example;
My data:
$scope.myData = {
"user": [
{ child[{......}],
..........
}
],"user2": [
{
child[{......}],
.........
}
],...........
The following works
console.log("lenght:"+$scope.myData.user[0].child.length);
but I want to use a variable instead of user[0], because it is dynamic, it changes every time.
Similar to
var m=user;
console.log("lenght:"+$scope.myData.m[0].child.length);

How about this?
var m = 'user';
console.log("lenght:"+ $scope.myData[m][0].child.length );

Related

How to get info from json using a variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
So I'm trying to get data from a JSON and I declared a variable and want to use the thing from that variable to get info from the JSON I'm trying to do this but I don't know what the problem is
let e = "KA8AL9AE";
let codes = '{"T9R3P5YE": "10", "8HAW69VC": "20"}'
codes = JSON.parse(codes)
console.log(codes.e)
I've tried doing
let e = "KA8AL9AE";
let codes = '{"T9R3P5YE": "10", "8HAW69VC": "20"}'
codes = JSON.parse(codes)
console.log(`${codes. + e}`)
But it didn't work, Is there a way to do this?
You could call the new key like:
codes[e]

object name is same as variable [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 3 years ago.
Let's say I have an object
const myArray = {
a : "hello"
}
and I have a string with the same name of that object
like
var type ="myArray";
when I do console.log(type);
output: myArray
but I want to out that object to the console which has the same name as the value of variable type.
How should I do that?
Thanks in advance
If it's a global variable, it will be stored in window.
So, you can do something like console.log(window[type]) to access to value.

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]

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.

get dynamic properties in javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
var tinymce_toolbar = {}
tinymce_toolbar.__default =
{
script_url: '/cms/libs/js/manual/renders/tiny_mce/tiny_mce.js',
};
tinymce_toolbar.__simple =
{
script_url: '/cms/libs/js/manual/renders/tiny_mce/tiny_mce_simple.js',
};
// Doesn't work
var t = $(this).find('input[name=toolbar]').first().val();
$('.RenderHtmlEditor').tinymce(tinymce_toolbar.t);
// works
var t = $(document).find('input[name=toolbar]').first().val();
$('.RenderHtmlEditor').tinymce(tinymce_toolbar.__default);
$('.RenderHtmlEditor').tinymce(tinymce_toolbar.__simple);
how i do it to be dynamic? Thanks
object['name'] is quite same way as object.name. simply assign a associative attribute and use it as a property.
Instead of dot notation,
tinymce_toolbar.t
Use subscript notation:
tinymce_toolbar[t]

Categories