Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew Lundy
13,269 PointsArray is printing as '[object Object]'
I had been working on this for a couple of hours last night and need some help with something that I’d figure be a lot simpler. I am creating a form for a password keeper I’m building, and when I try to return the array of accounts - I get [object Object]
When I console.log()
the single account, I get the following - Account {accountName: "Facebook", username: "Username", password: "Testpass", email: "", other: ""}
Which is what I expect to happen when adding the account to the accounts
array - except each array item is an Account
object.
With the current code, you can also call the individual properties such as console.log(account.password)
which would return whatever text was in the password input. Again, this is the behavior I would have expected to happen.
1 Answer

Dan Weru
47,649 PointsHello Andrew,
I took a quick look at your repo. If I'm right, I guess you were fiddling with the playground.js file. If that is indeed the case, then I would further guess that you're calling the console log inside this function
function addAccount() {
const accountName = document.getElementById('account').value;
const username = document.getElementById('user').value;
const password = document.getElementById('password').value;
const email = document.getElementById('email').value;
const other = document.getElementById('other').value;
let account = new Account(accountName, username, password, email, other);
accounts += account;
console.log(accounts);
}
accounts is an array of objects like so: [account1, account2, account3 ...]. Therefore, trying to log out the array will return [object][object] as expected.
If you wish to log out the details in the accounts array, you must loop through the array, then iterate over the objects values.
Here is an example of how I would do it:
// first loop through the accounts array.
// You could use a normal **for or while loop** .
// I prefer javascript methods though (forEach, map, filter, reduce)
accounts.forEach(function(account) {
// Now iterate through the values of the individual account objects
for (let key in account) {
let keyValue = account[key];
console.log(key, keyValue);
}
});
Andrew Lundy
13,269 PointsAndrew Lundy
13,269 PointsBeautiful. Thank you for the explanation.