JavaScript Array转换成Map

function serializeWithPrimaryKey(a, key)
{
    var o = {};
    $.each(a, function() {
        o[this[key]] = this;
    });
    return o;
}

var a = [{name: 'Fang', age: 23}, {name: 'Fun', age: 1}];
var o = serializeWithPrimaryKey(a, 'name');
// o => {Fang: {name: 'Fang', age: 23}, Fun: {name: 'Fun', age: 1}}