How to Remove Last Element from JSON Array in Javascript?

Nov 23, 2022 . Admin



Hello friends,

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

To remove the last element from the JSON array, you can use the pop(). The pop() method removes the last item of an array. The pop() method changes the original array. The pop() 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 Last Element from JSON Array in Javascript?</title>
</head>
<body>
<script type="text/javascript">

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

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

	const removeLast = jsonPlayer.pop();

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

I hope it will help you...

#Javascript