What's the deal with receiving [object Object] in my JavaScript JSON document?

When I use console.log(values), it returns "[object Object]" instead of logging the array as expected.

This is the code snippet I'm working with:

let values = {
    "coins": 0,
    "griffinFeathers": 0,
    "souvenir": 0,
    "cogs": 0,
    "cats": 0,
    "golems": 0,
    "champions": 0,
    "minotaurs": 0,
    "inquisitors": 0
}

JSON.stringify(FileLib.write("MinotaurLoot", "values.json", values));
function thing() {
    fileThing = JSON.stringify(FileLib.read("MinotaurLoot", "values.json"));
    if (griffin_thing_idk) fileThing.griffin++;    
}

console.log(values) // [object Object]

Answer №1

Examining:

JSON.stringify(FileLib.write("MinotaurLoot", "values.json", values));

JSON.stringify() must be eliminated because the resulting stringified version of your Object is not being stored anywhere or you could try this approach:

const stringifiedValues = 
    JSON.stringify(FileLib.write("MinotaurLoot", "values.json", values));

//Now you should be able to avoid [object Object] in console.log
console.log(stringifiedValues);

The reason why you see [object Object] is because JavaScript's default implementation of console.log() does not handle Complex Objects as nicely as primitive data types like String or Number.

@coderpc mentioned in their comment on your post that using JSON.stringify() before logging an object can help reveal its contents inside a console.log statement.

Give it a try and let us know if it resolves the issue.

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

Characteristics within the primary template element of a directive

I'm having an issue with the following directive code: .directive('myDirective', function () { restrict: 'E', replace: true, transclude: true, scope: { label: '@', ngModel: '=', ...

Tips for including a header with Apollo Client in a React Native app

In my React Native application, here's how I set up the Apollo client with an upload link: My goal is to include a header with a token value that will be sent with every request. However, I've had trouble finding an example specifically for Reac ...

How to stop parent event propagation in jQuery

I am facing a frustrating issue with my code. Every time I toggle a checkbox, the parent element's click event also triggers. I have experimented with various solutions like: event.stopImmediatePropagation(); event.stopPropagation(); event.preventD ...

Why does ksqldb fail to resolve the column name from a schema definition when using it in a SELECT query?

After obtaining this json-schema: { "$id": "http://schema-registry:8081/schemas/ids/1", "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "Locationvalue", "type" ...

Creating JSON Query Output in Codeigniter

My database table is called usermeta and it has the following structure : ----------------------------------------------------------- | ummeta_id | user_id | meta_key | meta_value | ----------------------------------------------------------- | ...

Deciphering JSON information using a LOOP

I'm having trouble parsing data from JSON using UNION ALL because I need to repeat the process multiple times. I tried using a LOOP but it didn't work as expected. My goal is to parse each element from an array in JSON into rows for statements. ...

Sending a parameter from a click event to a function

Struggling with a jQuery and AJAX issue all night. I am new to both and trying to implement something similar to this example. I have an edit button with the ID stored in a data-ID attribute. Here's an example of what my button looks like: <butto ...

Traverse JSON object as a string

I'm new to working with JavaScript. I have a JSON string that was created using the Google Gson API, and I'm passing this string to a JavaScript function. The JSON string looks like this: '{"validationCode":"V10291","caseNumber":"2010CF1010 ...

Include previous input as a "phantom" thumbnail to the slider element of type "range"

https://i.stack.imgur.com/JnuCN.png Using a tutorial from w3schools, I have customized a regular range slider. The objective is to send a new value command to an external MQTT-based home automation system and display the previous value as a ghost-thumb in ...

The contents of req.body resemble an empty {}

Does anyone know why the req.body is empty in this code snippet? Even though all form data is submitted, it doesn't seem to be recognized in the req.body. Strangely enough, it works perfectly fine in postman. Take a look at the server-side code: con ...

Customizing the width of the JQuery $ui.progressbar by overriding its properties

After spending some time, I successfully overrode the function that controls the width of the progress bar element in the jQuery UI widget version 1.12.1 from September 14, 2016. Initially, I needed the progress bar to completely fill a column in a table. ...

Cross-origin resource sharing (CORS) policy issue arises when the 'Access-Control-Allow-Origin' header is not included in the requested resource, especially when utilizing an iframe in a React

I am trying to link a website to a button using an iframe. This website allows access to all domain names. Below is the code for my button: <Button component={NavLink} activeClassName={classes.activeBtn} to="/searchEngine&qu ...

The error notification is not appearing in the correct location

I've been troubleshooting my jQuery error function for hours now (including the success function). I'm struggling to figure out how to display an error message only below the button that I click. To help clarify my issue, I've created a JSFi ...

I am attempting to integrate a datetimepicker onto my website, but I keep encountering an error. Can

Once upon a time, my website had a perfectly functioning datetimepicker. However, one day it suddenly stopped working. After some investigation, I realized that the JavaScript file I was referencing had been taken down. Determined to fix the issue, I visit ...

Printing to the console: the array is empty and contains just one element simultaneously

I've encountered an issue regarding the console.log function that seems related to a specific case I found... JavaScript array length problem Upon checking my console, I noticed this output: https://i.stack.imgur.com/AbCdE.png The code snippet in ...

Update Jquery Dialog's Button After Making an Ajax Request

I am facing an issue with my dialog window that contains some confirmation elements following a form submission: $('#newUserDialog').dialog({ autoOpen: false, width: 600, modal: true, resizable: false, cl ...

What is causing my grayscale function to only impact part of the canvas?

As a beginner programmer, I have been experimenting with creating a grayscale function in JavaScript for practice. This is the code I have come up with: <canvas width='400' height='400'></canvas> <script> var can ...

Sending a JSON array in an AJAX request results in receiving null values in an MVC application

I've been trying to send an array of JSON objects to my controller action, but it keeps showing up as null. Here's the JavaScript code I'm using: function UpdateSelected() { var items = {}; //Loop through grid / sub grids and crea ...

Unexpected unhandled_exception_processor in Google Chrome

I keep encountering a strange uncaught exception handler in Google Chrome. After updating all follow buttons to download JavaScript asynchronously, I noticed an error in the content.js file mentioned in the exception message which advises against polluting ...

Screen the received JSON data for a specific section of the website

Is there a way to extract Json data from a website using "selenium.captureNetworkTraffic("json")"? I need to refine the results for a specific grid on my website which contains multiple grids. How can I filter the data for a particular grid? Additionally ...