-2
var sub=["Maths","Chemistry","Physics"];

for(var i=0;i<sub.length;i++)
 {
   var sub[i]=[];  // this line has error 
 }

I want to create and get result as below:

Maths[],Chemistry[],Physics[]

If it is not possible to get in this way is there any alternate in Javascript to achieve the same

Saurya
  • 51
  • 1
  • 1
  • 6
  • What exactly do you mean by `Maths[]`? can you show an example of how that would be used? – Kevin B Mar 17 '15 at 18:24
  • Try `sub[i]=[]`, without `var`. You are assigning a property to an object, not declaring a variable. – Oriol Mar 17 '15 at 18:25
  • just use an object, eg: `var dataStore = { Math: [], Chemistry: [], Physics: [] };` – Yoshi Mar 17 '15 at 18:25
  • 2
    this is definitely an example of the XY problem. http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378. What you are demonstrating here isn't standard syntax, and you have given no context as to what it's being used for; Therefore you are asking about a broken solution to an unknown problem, but the solution you are presenting doesn't make any sense to fix. – Claies Mar 17 '15 at 18:28
  • just want to create empty array having names same as string stored in array(sub). – Saurya Mar 17 '15 at 18:31
  • it will be good if someone could help me out without using object. – Saurya Mar 17 '15 at 18:32
  • @Saurya: That's totally Okay, Claies just pointed that out, your question is way better than many others who DOES NOT attempt anything. You've shown us X instead of Y, but it's okay, next time, now, you know that you need to post Y :] – briosheje Mar 17 '15 at 18:33
  • In javascript there are no *variable variables*. You will always be creating variables in some "context". If none given it will implicitly be the global scope (`window` in a browser). Because of that, and because globals are evil, you're better of defining your own context => using an object you have control over. – Yoshi Mar 17 '15 at 18:33
  • right, your question is clear, it just doesn't make any practical sense without context. why are you trying to pass the variable names as an array of strings? why is using an object unacceptable? – Claies Mar 17 '15 at 18:34
  • @Yoshi appreciateyour solution so far i put my best effort and did same as u. – Saurya Mar 17 '15 at 18:35
  • @Claies using object is not unacceptable but i want another way to this problem. i have found solution using object. But thought if someone could find a way to do without object. – Saurya Mar 17 '15 at 18:37
  • so essentially, you don't have a practical reason for it, you just want to use a non-standard syntax for no reason at all, except to make your code harder to debug, and confusing to anyone who has to maintain it in the future? Also, you haven't really described a problem that needs solving; rather, a syntax you want to use. – Claies Mar 17 '15 at 18:39
  • 1
    There are propably whole book chapters about this topic, so a comment will not suffice, but trust me and the others here: There is almost certainly no good reason to create such variables without an object (or more generally: a defined scope). – Yoshi Mar 17 '15 at 18:39
  • @Kevin B Math[].. is empty array created after executing the code – Saurya Mar 17 '15 at 18:40
  • So, you would expect to use it with, say, `Math[5] = "foobar"`? or... – Kevin B Mar 17 '15 at 18:42
  • var sub=["Maths","Chemistry","Physics"]; var b={}; for(var i=0;i – Saurya Mar 17 '15 at 18:43

2 Answers2

3
var sub=["Maths","Chemistry","Physics"];
var result = {};

for(var i=0;i<sub.length;i++)
{
    result[sub[i]] = [];
}

I also recommend you read this article which has a good explanation on how to use objects as associative arrays.

Catalin MUNTEANU
  • 5,618
  • 2
  • 35
  • 43
0

Hopefully this example helps in addition to the above responses. You can past this code in a browser console and play around with it.

var dict = {Math:[], Physics:[], Chemistry:[]};

dict["Math"] = [0, 1, 2];

dict["Chemistry"] = ["organics", "biochemistry"];

dict["Physics"] = ["kinematics", "vectors"];

/*retrieve code by typing following one by one*/

dict["Math"]

dict["Chemistry"]

dict["Physics"]
kinjaldave
  • 145
  • 1
  • 13