passing key value pairs from one object to another

PHOTO EMBED

Wed Dec 25 2024 22:47:37 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #oop #keys #loop

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30,
  isEmployed: true,
  hobbies: ["reading", "traveling", "coding"],
  address: {
    street: "123 Main St",
    city: "Anytown",
    country: "USA",
  },
  greet: function () {
    return `Hello, my name is ${this.firstName} ${this.lastName}.`;
  },
};

const newObj = {};

for (let key in person) {
  if (key === "firstName" || key === "lastName") {
    newObj[key] = person[key];
  }
}

console.log({ newObj });
content_copyCOPY