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
Post a Comment