-3

I'm wondering why my variables do not recognized into my object title? I'm using query variable like this :

var query_type = req.query.type; // age (12)
var query_search = req.query.search; // name (robert)

var map = {
    query_type : query_search 
}

When I try to print the map, the output has something wrong.

console.dir(map);
{query_type : "robert"}

Why my query_type do not recognize in my object? I want to print like below :

{"age" : "robert"}
Richard
  • 351
  • 4
  • 17
  • It's unclear why that output was unexpected, you want `{ [query_type]: query_search }`. – jonrsharpe Mar 06 '19 at 08:00
  • 1
    or Possible duplicate of https://stackoverflow.com/questions/11508463/javascript-set-object-key-by-variable – AZ_ Mar 06 '19 at 08:02

1 Answers1

0

You need to use a dynamic property name with square bracket notation:

var map = {
    [query_type]: query_search
};
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79