I have numbers of a dynamically generated array in my script such as below:
var abc = {
'Lorem' = 'Ipsum is simply dummy text of the printing and typesetting industry.',
'Why' = 'but also the leap into electronic typesetting',
'Where' = 'making it over 2000 years old.'
}
var def = {
'Lore' = 'Ipsum is simply dummy text of the printing and typesetting industry.',
'hy' = 'but also the leap into electronic typesetting',
'Whre' = 'making it over 2000 years old.'
}
var ghi = {
'Lrem' = 'Ipsum is simply dummy text of the printing and typesetting industry.',
'Wh' = 'but also the leap into electronic typesetting',
'Were' = 'making it over 2000 years old.'
}
Now I need to get value from an array, I got the name of the array dynamically from a user and I store it in a variable like array_name.
I was trying to get value from variable like
var array_name = `abc`;
console.log(array_name['lorem']);
it gives me undefined as a response. Also, try to store value in hidden field and get value from textbox, but it hadn't work for me:
console.log(($('#array_name').val()['lorem']);
Please help me get value from array.
3 Answers 3
honestly I don't really like to use eval, if your random variable are globally scoped you can access it with window[array_name]['Lorem'], otherwise if they are limited inside a function or similiar, I suggest you to use a different approach, something like
var randomVars = {};
randomVars.abc = {
'Lorem': 'Ipsum is simply dummy text of the printing and typesetting industry.',
'Why' : 'but also the leap into electronic typesetting',
'Where' : 'making it over 2000 years old.'
}
randomVars.def = {
'Lore': 'Ipsum is simply dummy text of the printing and typesetting industry.',
'hy' :'but also the leap into electronic typesetting',
'Whre' : 'making it over 2000 years old.'
}
randomVars.ghi = {
'Lrem' : 'Ipsum is simply dummy text of the printing and typesetting industry.',
'Wh' : 'but also the leap into electronic typesetting',
'Were' : 'making it over 2000 years old.'
}
in this way you can call your text like randomVars[array_name]['Lorem']
Comments
Dont assign array_name to a string.
var abc = {
'Lorem': 'Ipsum is simply dummy text of the printing and typesetting industry.',
'Why' : 'but also the leap into electronic typesetting',
'Where' : 'making it over 2000 years old.'
}
var def = {
'Lore': 'Ipsum is simply dummy text of the printing and typesetting industry.',
'hy' :'but also the leap into electronic typesetting',
'Whre' : 'making it over 2000 years old.'
}
var ghi = {
'Lrem' : 'Ipsum is simply dummy text of the printing and typesetting industry.',
'Wh' : 'but also the leap into electronic typesetting',
'Were' : 'making it over 2000 years old.'
}
let new_name = abc
console.log(new_name['Lorem'])
2 Comments
Dont assign array_name to a string. then why not just console.log(abc['Lorem'])I would suggest you to create a object to hold object abc,def,..., then Bracket notation can be used to access the desired property using a string
let obj = {
abc: {
'Lorem': 'Ipsum is simply dummy text of the printing and typesetting industry.'
},
def: {
'Lore': 'Ipsum is simply dummy text of the printing and typesetting industry.'
},
ghi: {
'Lrem': 'Ipsum is simply dummy text of the printing and typesetting industry.'
}
}
console.log(obj['abc'])