-3

I have this array

query = { x: { y: z } }

I tried like this but jquery makes my vars as constant key

var x = "one";
var y = "two";
var z = "three";

var query = { x: { y: z } }
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
Younis Qadir
  • 295
  • 1
  • 3
  • 16

3 Answers3

0

You can use bracket notation to achieve that.

var query = {}, obj = {}; // objects
obj[y] = z; 
query[x] = obj;
console.log(query); 
Amit Joki
  • 56,285
  • 7
  • 72
  • 91
  • @Younisbarznji glad it worked.. do mark any answer as answer by clicking the tick mark which helped you the most – Amit Joki Dec 01 '14 at 09:46
0

Try this:-

var query; 
query[x]={};
query[x][y]=z;
Indranil Mondal
  • 2,727
  • 3
  • 24
  • 39
0

It seems you want to create a hashtable and use some variables as keys. You can do this by creating a hashtable and then setting values as follows

var query = {},
    x = 'one',
    y = 'two',
    z = 'three';

query[x] = {};
query[x][y] = z
Aleksi Yrttiaho
  • 7,856
  • 27
  • 36