Convert string value to object property name

Use the bracket notation like this:

objPosition["title"] = "Whatever";

you can also set the object's key like this

var property = "title"
objPosition[property] = "something";

use bracket notation: objPosition['title'] = "whatever";

so:

var objPosition = {}, ttl = 'title';
objPosition[ttl] = 'whatever'; 

[edit 11/2019: es20xx]

let objPosition = {};
const ttl = 'title';
// [...]
objPosition = {...objPosition, [ttl]: "whatever"};
console.log(objPosition);

Use ['propname']:

objPosition[txtCol] = "whatever";

Demo: http://jsfiddle.net/hr7XW/

Tags:

Javascript