Why does JSON.stringify() sometimes produce an empty result for certain objects?

I've been pondering over this very fundamental question, but the answer still eludes me.

What is the reason for the screen object being empty when stringified?

Could it be that JSON.stringify() requires some form of read/write access to its input?

let a = {foo: 'one', bar: 2};


console.log(JSON.stringify(a));
console.log(JSON.stringify(screen));

Answer №1

FROM Mozilla Developer Network

When it comes to serializing Object instances such as Map, Set, WeakMap, and WeakSet, only their enumerable properties will be included.

Visit https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable

    console.log((window.screen));
    console.log(JSON.stringify(window.screen));
    console.log(window.propertyIsEnumerable(screen));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the functionality of utilizing dual loops with v-for in Vue.js

I am facing an issue with my table code where I need to repeat it 6 times to match the day column above. I would like to use v-for and make use of a variable called "count" which represents the number of days. Can someone assist me with this task? <tabl ...

JavaScript code for displaying mouse positions and Geolocation using OpenLayers, along with a Geocodezip demonstration

I have attempted to merge Geolocation and Mouse Position Display (longitude/latitude; outside of map) from the OpenLayers and geocodezip site, but I am facing issues. The Mouse Position Display works correctly, however, Geolocation does not seem to work. U ...

Node-fetch enables dynamic requests

Seeking to retrieve real-time data from a fast-updating API has posed a challenge for me. The issue lies in my code constantly returning the same value. I've experimented with two approaches: var fetch = require("node-fetch"); for(let i=0; i<5; i+ ...

Alter the position of the anchor object in the center using JavaScript upon page load

When my page loads, I want to automatically jump to a specific anchor tag and ensure that the anchor object is centered in the window. Initially, I implemented a basic function to achieve this: <body onload="goToAnchor();"> <script type="text/ja ...

Merge a dropdown menu with an alphabetically arranged list that is interactive with clickable options

I am still learning HTML and Javascript but I'm doing my best. Currently, I am facing a challenge where I need to create a button that, when clicked, opens a dropdown menu containing a table of data. The user should then be able to select a number fr ...

How can I find out the identification of the control that is already linked to the element?

Currently, I am in the process of developing a web application using asp.net and c#. As part of this project, I am utilizing sql server reporting services (ssrs) to generate reports, which are then presented on the website. The method of presenting these r ...

Add a JavaScript library to the header directly from the body of a webpage, ensuring it is exclusive to a single

I am using the Google Charts JS library on a single page within my project, with external and global headers and footers. The head tags are located in a head.php file, where all required JS libraries are included. The structure of my pages is as follows: ...

The specialized element encountered an issue with mapping

For a while now, I've been trying to create my own custom component but it's not working as expected. The interaction seems fine, but for some reason, it only renders the last item in the arrays. export default class MyComponent extends Comp ...

Transform numbers into elements of an array and replace undefined with an empty array

Check out this Plunker example where a filter is implemented to allow select box options to be selected only once: http://plnkr.co/edit/BBqnTlxobUpiYxfhyJuj?p=preview .filter('arrayDiff', function() { return function(array, diff) { console.log ...

The outcome of the returned function is an array with unspecified results obtained from the original

I've been working on creating a simple function in ES5 to deep flatten an array. The current implementation appears to work, but it seems suboptimal because the res results array is defined outside of the actual flatten function. var arr = [1, ...

Changing text inside ion-header-bar directive

When utilizing the ion-header-bar directive, I have the left side designated as class="button", the middle section containing <h1> with the word "Recent", and the right side as <ng-icon>. The text on the left side is dynamically generated usin ...

The 'ObjectID' property is not present in the 'CollectionStatic' data type

Encountering an issue with the MongoDB npm module: mongoId = new Mongo.Collection.ObjectID()._str; Attached is a screenshot for reference. Appreciate any assistance.https://i.sstatic.net/EHdMo.png ...

Tips for incorporating choices into a newly created dynamically generated div

In the process of creating a website builder, I encountered an issue. I need to allow users to add boxes dynamically to the main section and have related options appear when a box is clicked. The problem arises when multiple boxes are added: clicking on th ...

Bringing in an array of data that has been stored within an

As a newcomer to Angular and JS, I am struggling with incorporating an array stored in a variable inside a factory into my directive. The array, labeled as 'this.insights', needs to be imported. I suspect that I may need to assign the array like ...

Detecting browser or tab closure in Node/Express Application: A comprehensive guide

As I'm developing a Node + Express MVC application, I am looking for a way to automatically shut down the Express server when the browser or tab is closed. While I know I can achieve this using a vanilla JS script with the 'beforeunload' eve ...

Exploring jQuery AJAX Attributes during ajaxStart and ajaxStop event handlers

With my project consisting of over 40 ajax webservice calls, I am looking to incorporate additional debugging features. One such feature is a timing method which I have already developed using my Timer class/object in Javascript. I'm seeking assistan ...

Alter the middle number in a sequence using JavaScript or Node.js

let str = "demo-test-1-0-4_testing.pdf"; I have a string that I need to replace with the word verified My desired output should look like this str= demo-test-verified_testing.pdf I am able to achieve this if the numbers are at the end of the s ...

Creating multi-dimensional arrays using array lists with Axios and Vue.js

My knowledge of axios and JavaScript is limited, and I find myself struggling with creating a multi-dimensional array. 1. This is how I want my data to be structured : https://i.stack.imgur.com/kboNU.png userList: [ { ...

Ember - connecting to a JSON data source

Recently, I have been following a tutorial/example from codeschool and everything is going well. However, the example code includes the line: App.ApplicationAdapter = DS.FixtureAdapter.extend(); Now, I want to maintain all the existing functionality but ...

Attempting deletion with Node.js's Mongoose Framework

Having some trouble with the following code snippet. It doesn't seem to be functioning correctly and is resulting in a 404 error. Any insights on how to troubleshoot this issue? app.delete("/tm/v1/tasks", (req,res) => { Task.findOneAndDelete ...