This function converts an array of objects into a single object by using the Object.assign()
method, which is used to copy values from one or more source objects to a target object.
JavaScript:
let arrToObj = arr => Object.assign({}, ...arr);
// Example
let arr = [{ a: 1 }, { b: 2 }, { c: 3 }];
console.log(arrToObj(arr)); // { a: 1, b: 2, c: 3 }
TypeScript:
let arrToObj = (arr: Array<Object>): Object => Object.assign({}, ...arr);
// Example
let arr = [{ a: 1 }, { b: 2 }, { c: 3 }];
console.log(arrToObj(arr)); // { a: 1, b: 2, c: 3 }