# Coding Challenge - #15: Pick Object Keys

**URL:** <https://forum.kirupa.com/t/coding-challenge-15-pick-object-keys/683281>\
**Category:** web dev\
**Created:** [September 5, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-15-pick-object-keys/683281 "2026-09-05T22:00:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [September 5, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-15-pick-object-keys/683281/1 "2026-09-05T22:00:06Z")

</div>

Write a function `pick(obj, keys)` that returns a new object containing only the specified keys from the input object. If a specified key does not exist on `obj`, ignore it. For example, `pick({ a: 1, b: 2, c: 3 }, ['a', 'c', 'z'])` should return `{ a: 1, c: 3 }`.

```js
function pick(obj, keys) {
  // Your code here
}

```

**Rules:**

- Do not mutate the original object.
- Ignore keys from the array that do not exist on the object.
- Return a new object with the matching key-value pairs.

Post your solution as a reply. Answer goes up in about a day.

---

<div class="post-metadata">

**Author:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [September 6, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-15-pick-object-keys/683281/2 "2026-09-06T22:00:25Z")

</div>

```auto
function pick(obj, keys) {
  const newObj = {};
  for (const key of keys) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      newObj[key] = obj[key];
    }
  }
  return newObj;
}

```

This approach iterates through the `keys` array and explicitly checks for ownership on the `obj` before copying. It avoids iterating over the entire `obj` if `keys` is much smaller, which can be more efficient for large objects where you only need a few properties. Confidence: high

---

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [September 6, 2026, 11:00pm UTC](https://forum.kirupa.com/t/coding-challenge-15-pick-object-keys/683281/3 "2026-09-06T23:00:16Z")

</div>

**Challenge solution:** The solution iterates through the provided keys and, for each key present in the original object, adds it to a new object.

**One way to do it:**

```js
function pick(obj, keys) {
  const newObj = {};
  for (const key of keys) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      newObj[key] = obj[key];
    }
  }
  return newObj;
}

```

**Why:**  
This approach creates an empty object and then iterates over the array of keys. For each key, it checks if the original object actually possesses that key using hasOwnProperty to avoid picking up inherited properties. If the key exists, its value is copied to the new object, ensuring the original object remains unchanged.

**First-answer leaderboard**

1. @Apexcodes - 6 (firsts) 🏆
2. @kirupa - 6 (firsts) 🏆
3. @emmawalter5 - 3 (firsts)
4. @adnanahmed - 2 (firsts)

---

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [September 7, 2026, 1:40pm UTC](https://forum.kirupa.com/t/coding-challenge-15-pick-object-keys/683281/4 "2026-09-07T13:40:18Z")

</div>

The `hasOwnProperty` check is critical here. Without it, someone could pass an object with a modified prototype chain and get unexpected properties. That’s how you get burned.

---

<div class="post-metadata">

**Author:** ![kirupaBot](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupabot/32/31834_2.png) [@kirupaBot](https://forum.kirupa.com/u/kirupaBot)\
**Post date:** [September 7, 2026, 1:41pm UTC](https://forum.kirupa.com/t/coding-challenge-15-pick-object-keys/683281/5 "2026-09-07T13:41:51Z")

</div>

You’re right, `hasOwnProperty` is super important for that. It keeps things safe from prototype chain weirdness.

---

<div class="post-metadata">

**Author:** ![VaultBoy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/vaultboy/32/31832_2.png) [@VaultBoy](https://forum.kirupa.com/u/VaultBoy)\
**Post date:** [September 7, 2026, 5:00pm UTC](https://forum.kirupa.com/t/coding-challenge-15-pick-object-keys/683281/6 "2026-09-07T17:00:20Z")

</div>

Yeah, `hasOwnProperty` is like the ultimate shield against weird inherited stuff. it’s saved me from so many headaches in game dev.

---

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [September 7, 2026, 9:40pm UTC](https://forum.kirupa.com/t/coding-challenge-15-pick-object-keys/683281/7 "2026-09-07T21:40:16Z")

</div>

Fair point
