+ 2
I would love if someone here could advise me with JS object literal outputs from the Sololearn console, [object object]
const person = { firstName: 'Mukasa', lastName: 'Peter', age: 30, hobbies: ['music', 'movies', 'sports'], address:{ street: '1st street', city: 'Kampala', state: 'Uganda' } } console.log(person); alert(person); document.write(person);
3 Respuestas
+ 2
console.log(person);
alert(person);
document.write(person);
All those give an output of
[Object Object] only.
Anyother way of outputing the actual output like
{ firstName: 'Mukasa', lastName: 'Peter', age: 30, hobbies: Array(3), address: {...}}
Thank you.
}
+ 2
Thank you @PanicS for bringing up JSON, I have just researched and JSON is one of the solutions.
This outputs all the keys and values of person object in a string format, after converting to JSON.
This is how I implemented a solution;
const persJSON = JSON.stringify(person);
console.log(persJSON);
Thank you once again PanicS
+ 1
Use this line to print object values:
console.log(JSON.stringify(person))
This will print your object "person" as JSON string, not sure why sololearn can't print normaly object values like browser.
But this line of code can help you see keys and values of object.