0

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.

0

3 Answers 3

1

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']

Sign up to request clarification or add additional context in comments.

Comments

1

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

The OP wants it to be a string, you are just referring to the obj. Since you say Dont assign array_name to a string. then why not just console.log(abc['Lorem'])
You can convert a string to object as well. @CarstenLøvboAndersen
0

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'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.