Menu
Coddy logo textTech

Remove Keys

Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 20 of 65.

There are many ways to delete a key in JSON, here are some of them:

Consider the following JSON:

const obj = {
  name: "Alex",
  age: 30,
  preferences: ["sports", "music"]
}

Using the delete keyword:

delete obj["name"]

Using the spread operator:

const {name, ...filteredObj} = obj;
// filteredObj has two keys: age, preferences
challenge icon

Challenge

Easy

Create a function named removeKeys that takes two parameters:

  1. An object
  2. An array of strings (keys to be removed)

The function should return a new object with the specified keys removed.

Cheat sheet

There are multiple ways to delete keys from a JSON object:

Using the delete keyword:

delete obj["name"]

Using the spread operator with destructuring:

const {name, ...filteredObj} = obj;
// filteredObj contains all keys except 'name'

Try it yourself

function removeKeys(obj, keysToRemove) {
    // Write code here
}
// Do not write anything outside function
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow