How to Remove JSON Key Value in Javascript?

Nov 04, 2022 . Admin



Hello friends,

In this tutorial, you will learn how to JSON remove the key value in javascript. This article goes into detail on removing key-value pairs from a JSON object. you will learn to remove JSON elements in javascript. you can see the removed JSON key value if the value inside the object exists.

JSON attributes are represented as key-value pairs something similar to associative arrays. Plus JSON comes in all sorts of representations - simple, nested, or arrays. The simple format contains a set of attributes. In order to remove an attribute from JSON you have to use the JS delete method. This will delete the JSON attribute with the specific key.

So, let's see bellow solution:

Example
index.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How to Remove JSON Key Value in Javascript?</title>
</head>
<body>
<script type="text/javascript">
    var data = [
        {
            "name":"John Smith",
            "age":"45",
            "department":"Administration",
            "company":"ABC Corp"
        },
        {
            "name":"Peter Jason",
            "age":"26",
            "department":"Administration",
            "company":"ABC Corp"
        },
        {
            "name":"Alice Ray",
            "age":"34",
            "department":"Administration",
            "company":"ABC Corp"
        }
    ];

    for (var i=0; i< data.length; i++) {
        delete data[i].age;
    }
    console.log(JSON.stringify(data));
</script>
</body>
</html>
Check The Console For Output:
[{"name":"John Smith","department":"Administration","company":"ABC Corp"},
{"name":"Peter Jason","department":"Administration","company":"ABC Corp"},
{"name":"Alice Ray","department":"Administration","company":"ABC Corp"}]

I hope it will help you...

#Javascript