princesse princesse - 9 months ago 32
Javascript Question

how to get dynamically created object inserted into another object

Good morning!
i have two dynamically created objects using form fields values. They are object literals which look like this:

var courses = {};
course['name'] = $('#course').val();
var students = {};
students['first'] = $('#first_name').val();
students['last'] = $('#last_name').val();
students['age'] = $('age').val();

I would like to insert them in a single object literal
var person= {}

i need the two objects into one because I need to stringify it and send via ajax to my process page. Could you please help me?


You can populate an object as you are creating it:

var persons = {
    courses: {
        name: $('#course').val(),
    students: {
        name: $('#first_name').val(),
        last: $('#last_name').val(),
        age: $('#age').val(),

If you want to use the objects that you already have then you can do so:

var persons = {
    courses: courses,
    students: students,