Home  >  Article  >  Web Front-end  >  JavaScript: How to remove the key-value pair corresponding to a given key from an object?

JavaScript: How to remove the key-value pair corresponding to a given key from an object?

PHPz
PHPzforward
2023-08-22 15:53:123276browse

JavaScript: How to remove the key-value pair corresponding to a given key from an object?

In JavaScript, objects can be created to store data in the form of key-value pairs. Data in an object can be accessed using dot notation (obj.key) or bracket notation (obj["key"]). Please refer to the example below −

let obj = { key1: "value1", key2: "value2", key3: "value" };
We can delete the key-value pairs corresponding to the given keys from the object, but before deleting, we need to ensure that these keys exist in the object In this tutorial, we will introduce 3 methods.

Use delete operator

The delete operator is used to delete the properties of an object. The delete operator does not delete the variable itself, but only its value.

Example

Please refer to the following example -

<!doctype html>
<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result"></div>
   <script>
      let obj = { key1: "value1", key2: "value2", key3: "value3" };
      delete obj.key2;
      document.getElementById("result").innerHTML = JSON.stringify(obj);
      console.log(obj)
   </script>
</body>
</html>

As can be seen from the above example, the delete operator only deletes the

of the value key and not the key itself.

Below is the line by line explanation of the above code −

key and not the key itself.

The following is a line-by-line explanation of the above code −

let obj = { key1: "value1", key2: "value2", key3: "value3" };

We have created an object containing 3 key-value pairs.

delete obj.key2;

delete operator is used to delete key-value pairs, where the key is "key2".

console.log(obj);

The output of the above code on the console will be: { key1: "value1", key3: "value3" } You can see that the key-value pair with key "key2" has been removed from the object.

Using the filter() method

The filter() method is used to create a new array from an existing array. See the example below:

Example




   Examples


   
<script> let obj = { key1: &quot;value1&quot;, key2: &quot;value2&quot;, key3: &quot;value3&quot; }; let newObj = Object.keys(obj) .filter(key => key != "key2") .reduce((acc, key) => { acc[key] = obj[key]; return acc; }, {}); document.getElementById("result").innerHTML = JSON.stringify(newObj); console.log(newObj) </script>

As you can see from the above example, the filter() method only removes the the key and not the key itself.

Below is the line by line explanation of the above code:

The key is the key itself.

The following is a line-by-line explanation of the above code:

let obj = { key1: "value1", key2: "value2", key3: "value3" };

We have created an object containing 3 key-value pairs.

let newObj = Object.keys(obj)
.filter(key => key != "key2")
.reduce((acc, key) => {
   acc[key] = obj[key];
   return acc;
}, {});

Object.keys() method is used to create an array containing object keys The filter() method is used to create a new array from an existing array. The key is Compare to "key2". If not equal, add the key-value pair to a new array The reduce() method is used to reduce an array into an object.

console.log(newObj);
The output of the above code will be:

{ key1: "value1", key3: "value3" }

. As you can see, The deleted key-value pair with key "key2" has been deleted from the object.

Use for...in loop

for...inLoop is used to traverse the properties of the object.

Example

Please refer to the following example −




   Examples


   
<script> let obj = { key1: &quot;value1&quot;, key2: &quot;value2&quot;, key3: &quot;value3&quot; }; for (let key in obj) { if (key == "key2") { delete obj[key]; } } document.getElementById("result").innerHTML = JSON.stringify(obj); console.log(obj) </script>

As can be seen from the above example, the for…in loop only deletes the value of the key and not the key itself.

Below line by-line explanation of the above code:

Not the key itself.

The following is a line-by-line explanation of the above code:

let obj = {key1: "value1", key2: "value2", key3: "value3"};

We have created an object containing 3 key-value pairs.

for (let key in obj) {
   if (key == "key2") {
      delete obj[key];
   }
}

Use a for…in loop to traverse the properties of the object. The key variable is used to store the key of the object. If key is "key2", remove the key-value pair from the object.

console.log(obj);
The output of the above code will be:

{ key1: "value1", key3: "value3" }

. As you can see, The deleted key-value pair with key name "key2" has been deleted from the object.

Conclusion

In this tutorial, we introduced 3 ways to delete key-value pairs corresponding to key-value pairs Given the key of an object. Delete operator, for...in loop and filter() method.

The above is the detailed content of JavaScript: How to remove the key-value pair corresponding to a given key from an object?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete