-1

I'm pretty sure this question is going to get negative response as many people have already asked it in SO. But trust me I have read every single answer of every single question none helped.

I know that in jquery you can put the array key name dynamically like this: somearray1["abc" + variable] but I'm not looking for that. I want to call the array name dynamically like:

var i=1;
console.log( "somearray" + i["abc" + variable] )

Can someone tell me how is it possible? I cannot put it in another array and call that as I'm building a very dynamic script, so I must need to call the array name dynamically.

Any help will be highly appreciated.

13
  • You could use eval() as your last resort. Commented Aug 23, 2016 at 20:16
  • but using eval is bad as per as I know due to security reason. Commented Aug 23, 2016 at 20:16
  • You tried this? stackoverflow.com/questions/22566160/… Commented Aug 23, 2016 at 20:17
  • how would you do i[....] if i is a number, not an array or object? Perhaps you need to clarify the question... Commented Aug 23, 2016 at 20:18
  • @RickS yes I saw that but it didn't help. As I said I cannot put it inside another array or things like that. I need to directly call it dynamically. Commented Aug 23, 2016 at 20:19

2 Answers 2

1

Normaly, your array depend from this.

this["somearray" + i]["abc" + variable]

var bob1 = [1,2,3];
var name = "bob";
console.log(this[name+"1"][0])

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

4 Comments

I will try this out and let you know. Thanks a lot for your answer.
Isn't this the same as window["somearray" + i]["abc" + variable]?
Depend of your scope. Windows is part of the main scope. This refer to the local scope and the first thinks that jquery do is to create is own scope.
@GeraldChablowski thanks a lot brother. Yah I see it. Actually it is 2am here and I must need to get some sleep as I haven't slept for 2 days now. So I really have no strength to change all of my code to test it. But thanks a lot for your snippet.
0

I'm not sure exactly what you're asking here from your example. Do you want to console.log the value held at a dynamically changing position of "somearray"? If so, array positions are numerical indices (e.g. somearray[1]) and cannot be accessed via strings.

Are you trying to access an object property (e.g. someObject1["abc" + variable]) alternatively?

If it's an object property you are trying to access with changing parameters, you might want to try using ES2015 template literal syntax.

let i = 1;
let someObjectName = `someObject${i}`;
console.log( someObjectName[`abc${variable}`] );

That way the concatenation of the object name and its property will happen dynamically. I hope this helps.

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.