0

Console.log(values) returns "[object Object]" instead of logging the array.

This is my code:

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]
2
  • 1
    Do this console.log(JSON.stringify(values)) Commented Oct 14, 2020 at 4:16
  • 1
    shouldn't you stringify content while writing ? FileLib.write("MinotaurLoot", "values.json",JSON.stringify( values)) Commented Oct 14, 2020 at 4:34

1 Answer 1

2

Looking at:

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

JSON.stringify() should either be removed as you're not storing the returned stringified version of your Object anywhere or do something like:

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

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

Now, the reason why you're getting [object Object] is due to the fact that JavaScript's default implementation of console.log() does not handle gracefully JavaScript Objects as they are a 'more complex' data structure than primitives like String or Number.

As pointed out by @coderpc in the comment on your question, you can access the inner contents of an object by using the JSON.stringify() method before wrapping it in a console.log statement.

Let us know if it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.