How to Remove JSON Null Values in Javascript?
Nov 08, 2022 . Admin
Hello friends,
This simple article demonstrates how to remove JSON null values in javascript, here you will learn to remove JSON null values with an example. This example will help you remove the null values of JSON. you will do the following things for javascript remove null values with an example.
To remove null values, you can use the for loop to iterate over the array of keys. Check if each value is equal to null and delete the null values using the delete operator.
So, let's see bellow solution:
Example:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>How to Remove JSON Null Values in Javascript?</title> </head> <body> <script type="text/javascript"> //create jsonPlayer var jsonPlayer = { player: null, name: 'Virat Kohli', jerseynumber: 18, } function remove(obj) { for (var propName in obj) { if (obj[propName] === null || obj[propName] === undefined) { delete obj[propName]; } } return obj } //print remove JSON null values console.log(remove(jsonPlayer)); </script> </body> </html>Check The Console For Output:
{name: 'Virat Kohli', jerseynumber: 18}
I hope it will help you...