A common use of JSON is to exchange data to/from a web server.
When sending data to a web server, the data has to be a string.
Convert a JavaScript object into a string with JSON.stringify()
.
In JSON, functions are not allowed as object values.
The JSON.stringify()
function will remove any functions from a JavaScript object, both the key and the value:
Example(1)
var obj = { name: "John", today: new Date(), city : "New York" }; var myJSON = JSON.stringify(obj);
This can be omitted if you convert your functions into strings before running the JSON.stringify()
function.
Complete Code For Remove Any Function From Array Using JSON Stringify
<!DOCTYPE html> <html> <head> <title>How To Remove Any Function From Array Using JSON Stringify</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <br> <div class="text-center"> <h1 id="color" style="color: tomato">How To Remove Any Function From Array Using JSON Stringify</h1> </div> <br> <div class="well"> <h2 id="demo1"></h2> </div> <script> var obj = { name: "John", today: new Date(), city : "New York" }; var myJSON = JSON.stringify(obj); document.getElementById("demo1").innerHTML = myJSON; </script> </div> </body> </html>