Javascript setting object with variable -


this question has answer here:

i'm trying set object using variable. here's have:

var sortby = 'post_date'; var sort = { sortby : 'asc' }; 

but when console.log(sort) object {sortby: "asc"}

how can set key of object value of variable?

prior es6 (the newest javascript standard), following:

var sortby = 'post_date'; var sort = {};  sort[sortby] = 'asc'; 

however, if made sure can use es6 features you're doing, possible:

var sortby = 'post_date';  var sort = {   [sortby]: 'asc }; 

see pages more information on es6 features: https://github.com/lukehoban/es6features


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -