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! While you're at it, check out some resources Treehouse students have shared here.
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
Joseph Wasden
20,407 PointsJavaScript Arrays showing as empty after pushing objects to them - NOT A LESSON QUESTION
Hi Everyone,
I've been tinkering away and breaking things, learning a lot as I go, but I've hit a road block.
I'm trying to build a forms app. I'm currently working on a page that is meant to let a user click buttons to define what form elements they would like to add to a form. (I intend to allow them to reorder the schema, and rename elements, but that's all for later).
anyhow, when a user clicks to add a form element, I am currently pushing some boilerplate values as an object to an array. The current thinking is, as the user adds different components to the form (header, textfield, etc) and makes changes to them, I can update the corresponding object in the array. then, when the form is all defined and ready to be completed, to JSON-ify the array elements and send it to be stored in a mongoDB database.
When I push elements to the array, they seem to go there. The issue is, however, that when I call the array in the console, it returns as empty. Any clues as to why that is?
Here's some snippets from my project
var db = [];
function dbPrepare( object ) {
//commented out bits that i've fiddled with, but am not currently decided on
//let dbObject = JSON.stringify(object);
//JSON.parse(dbObject);
//db.push(dbObject);
db.push(object);
console.log(db);
}
//variables of properties declared above, not shown here for brevity
let object = {
"position": position,
"type": "date",
"text": dateText,
"label": dateLabel
};
dbPrepare( object );
I've included the link to my current project code. (it's still rough.) TIA for any replies, or any criticisms/comments on my code. (Be gentle. ;) )
1 Answer
Jarrade Pierce
17,633 PointsThis worked for me
db = [];
function DbPrep(object){ db.push(object); console.log(object); }
object = { name: "name", age: 23 };
DbPrep(object);
Jarrade Pierce
17,633 PointsJarrade Pierce
17,633 PointsThis worked for me I hope im understanding your question.
db = [];
function DbPrep(object){ db.push(object); console.log(object); }
object = { name: "name", age: 23 };
DbPrep(object);