-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
  • 331,213
  • 40
  • 305
  • 339
Younis Qadir
  • 315
  • 1
  • 4
  • 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
  • 58,320
  • 7
  • 77
  • 95
  • @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,799
  • 3
  • 25
  • 40
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
  • 8,266
  • 29
  • 36