How to can get this
var myString = 'a,b';
in the following in most efficient way
var myObject = { a:1, b:1};
I need 1 to be associated with each param. Thank you.
Supposing your string is really defined as
var myString = 'a,b';
then you can get your object as
var obj = {};
var t = myString.split(',');
for (var i=0; i<t.length; i++) obj[t[i]] = 1;
This would make an obj just like
var obj = { a:1, b:1};
Note that I didn't get your goal so this might be useless...
A side remark :
JSON is a text format used for data exchange. There is nothing like a JSON object. { a:1, b:1} is just a plain javascript object with two properties.
If what you want is really JSON, you may do
var myJSON = JSON.stringify(obj);
Tis would be equivalent to
var myJSON = '{"a":1,"b":1}';
You can use .split() and .reduce() for this.
var result = myString.split(',')
.reduce(function(obj, key) {
return obj[key] = 1, obj;
}, {});
You'll need a shim for older JavaScript implementations.
// Abrakadabra, but it might fail.