How to Remove First Element from JSON Array in Javascript?

Nov 22, 2022 . Admin



Hello friends,

In this tutorial, I will show you how to remove the first element from a JSON array in javascript. you will learn to remove the first element from the JSON array. It's a simple example of removing the first element from JSON with an example. Here, Creating a basic example of removingelement from the JSON array with an example in javascript.

To remove the first element from JSON array, you can use the shift(). The shift() method removes the first item of an array. The shift() method changes the original array. The shift() method returns the shifted element.

So, let's see bellow solution:

Example:
index.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>How to Remove First Element from JSON Array in Javascript?</title>
</head>
<body>
<script type="text/javascript">

    //create jsonPlayer 
    const jsonPlayer = ['Hardik Pandya', 'Rishabh Pant', 'Ishan Kishan'];

    // jsonPlayer before shift: ['Hardik Pandya', 'Rishabh Pant', 'Ishan Kishan']
    console.log('jsonPlayer before shift:', JSON.stringify(jsonPlayer));

    //shifted element
    const shifted = jsonPlayer.shift();

    // jsonPlayer after shift: ['Rishabh Pant', 'Ishan Kishan']
    console.log('jsonPlayer after shift:', jsonPlayer);
    
    // Removed element: Hardik Pandya
    console.log('Removed element:', shifted);
    
</script>
</body>
</html>
Output:
jsonPlayer before shift: ["Hardik Pandya","Rishabh Pant","Ishan Kishan"]
jsonPlayer after shift:['Rishabh Pant', 'Ishan Kishan']
Removed element: Hardik Pandya

I hope it will help you...

#Javascript