怎么样:
keys = ['foo', 'bar', 'qux'];
values = ['1', '2', '3'];
some = {};
_.each(keys,function(k,i){some[k] = values[i];});
要完整:另一种方法可能是:
_.zip(['foo', 'bar', 'qux'],['1', '2', '3'])
.map(function(v){this[v[0]]=v[1];}, some = {});
作为记录,没有下划线你可以扩展 Array.prototype:
Array.prototype.toObj = function(values){
values = values || this.map(function(v){return true;});
var some;
this .map(function(v){return [v,this.shift()]},values)
.map(function(v){this[v[0]]=v[1];},some = {});
return some;
};
// usage
var some = ['foo', 'bar', 'qux'].toObj(['1', '2', '3']);
见jsfiddle