There isn't a native method.
Here you can find two ways of achieving it
The first method, m1, will validate the length of your arrays, if it doesn't match it will throw an exception.
function m1(keys, values)
{
// in case the arrays have different sizes
if (keys.length != values.length)
throw new Error("Arrays are not of the same size");
var result = {};
for (var i in keys)
{
result[keys[i]] = values[i];
}
return result;
}
The second method, m2, will allow you to have more keys than values, and fill the missing values with null.
function m2(keys, values)
{
// alternative handling for different sizes
if (keys.length < values.length)
throw new Error("You can't have more values than keys");
var result = {};
for (var i in keys)
{
var val = null;
if (i < values.length)
val = values[i];
result[keys[i]] = val;
}
return result;
}
Both methods return an object, to have an object exactly as you described you will can do this:
var x = {};
x.obj = m1([1, 2, 3], ["a", "b", "c"]);
For a compact version (without validations) you can use:
function m(k,v){r={};for (i in k)r[k[i]]=v[i];return r}
Usage:
var x = { obj: m([1,2],["A","B"]) };
Change m to the desired function name.
Reference