Can I Set The Value Of A Property Dynamically?
I have the following: var data.roles = 'Admin:Xxxx:Data'; for (role in data.roles.split(':')) { if (role == 'Admin') { user.data.role.isAdmin = true } if (
Solution 1:
Since split returns an Array, you could use forEach:
vardata = {roles: "Admin:Xxxx:Data"};
var user = {data: {role:{}}};
data.roles.split(':').forEach(function(v) {
user.data.role['is' + v] = true;
})
console.log(user.data.role.isXxxx); // true
There is a polyfill at MDN for browsers without forEach.
Post a Comment for "Can I Set The Value Of A Property Dynamically?"