I'm trying to cleanse some data and place an array of objects inside of an object. Here's my array of objects.
careerStats:
[
{
gp: '35',
gs: '34',
mpg: '33.2',
fg: '.515',
tp: '1.5',
ft: '1.4',
rpg: '0.5',
apg: '0.7',
bpg: '.692',
spg: '0.7',
ppg: '3.6'
},
{
gp: '22',
gs: '22',
mpg: '36.7',
fg: '.504',
tp: '5.5',
ft: '2.4',
rpg: '1.7',
apg: '2.0',
bpg: '.822',
spg: '1.5',
ppg: '6.5'
},
{
gp: '57',
gs: '56',
mpg: '34.6',
fg: '.509',
tp: '3.1',
ft: '1.8',
rpg: '1.0',
apg: '1.2',
bpg: '.775',
spg: '1.0',
ppg: '4.7'
}
]
And here's how I'm placing the careerStats array of objects inside of a player array.
player:
player.push(
{
name: "",
image: "",
position: "",
description: "",
dob: "",
hometown: "",
country: "",
height_feet: "",
height_inches: "",
weight: "",
season: careerStats
});
This does not return any sort of error; however, when I log the player to the console here's what displays:
[
{
name: '',
image: '',
position: '',
description: '',
dob: '',
hometown: '',
country: '',
height_feet: '',
height_inches: '',
weight: '',
season: [ [Object], [Object], [Object] ]
}
]
Since I know the data is fine when it's in the careerStats array, this leads me to two questions:
- Am I doing something incorrectly to receive this output as [ [Object], [Object], [Object] ]? ANSWER: Resolved by changing to console.log(JSON.stringify(player))
If this is the expected output and the data is indeed there, how can I write this to a JSON file where the data displays instead of "[object Object]"?
JSON.stringify(player); fs.appendFile('myjsonfile.json', player, function (err) { if (err) throw err; console.log('Saved!'); });
Thanks in advance for the help!
console.log(JSON.stringify(player))you should see what you expect.console.logwill only recurse into an array or object so many levels before it prints generic type strings. This is so you can print complex objects with deep levels of nested data without it taking up thousands of lines when all you wanted was to view some of the top-level fields.