CC-10: Reading Objects

with Carmen Salas • 2024/10/21

slides

Objects

too eepy to take notes

Code Challenge

  1. Write a function named getAllKeys that takes in an object argument and logs all the keys in the given argument to the console.

const getAllKeys = (obj) => {
  Object.keys(obj).forEach((key) => {
    console.log(key);
  });
};

// getAllKeys(associateInstructorAges) // will log to the console: "carmen", "itzel", "zo"
  1. Write a function named getAllValues that takes in an object argument and logs the value of every key in the given argument to the console.

const getAllValues = (obj) => {
  Object.values(obj).forEach((value) => {
    console.log(value);
  });
};

// getAllValues(associateInstructorAges); // will log to the console: 22, 22, 23

BONUS: 3. Write a function named sumAllValues that takes in an object argument and returns the sum of all the values of every key in the given argument.

const sumAllValues = (obj) => {
  return Object.values(obj).reduce((sum, val) => sum + val);
};

// console.log(sumAllValues(associateInstructorAges));

Last updated