Javascript: How to convert an array of objects to an object