Array methods - Task 13

Create keyed object from array

by fredericklim

HTML

<!doctype html>
<html>
<script>

let users = [
  {id: 'john', name: "John Smith", age: 20},
  {id: 'ann', name: "Ann Smith", age: 24},
  {id: 'pete', name: "Pete Peterson", age: 31},
];

let usersById = groupById(users);

console.log(usersById);

/*
// after the call we should have:

usersById = {
  john: {id: 'john', name: "John Smith", age: 20},
  ann: {id: 'ann', name: "Ann Smith", age: 24},
  pete: {id: 'pete', name: "Pete Peterson", age: 31},
}
*/

  function groupById(array) {
    return array.reduce((o, value) => {
      o[value.id] = value;
      return o;
    }, {})
  }

</script>
</html>