簡體   English   中英

將對象文字插入到對象數組中

[英]insert object literals pushed into array in an object

早上好! 我從表單值生成對象文字並將其存儲在數組中,如下所示

 var persons = {};// I want all the objects in the array to be inserted here so that I can stringify it and send via Ajax var array = [];// where I store the object literals var courses = {}; course['name'] = $('#course').val(); array.push(courses); var students = {}; students['first'] = $('#first_name').val(); students['last'] = $('#last_name').val(); students['age'] = $('age').val(); array.push(courses); // I tried this after the help I got this morning but it does not create multiple objects. When there are many successive values, the data sent is empty var persons = { courses: { name: $('#course').val(), }, students: { name: $('#first_name').val(), last: $('#last_name').val(), age: $('#age').val(), }, }; 

請問有人知道我如何創建幾個學生並將他們與課程名稱放在同一對象中嗎?

如下更改代碼

 courses = {
        name: $('#course').val(),
        students: []
 };

添加學生,您可以:

courses.students.push({
        name: $('#first_name').val(),
        last: $('#last_name').val(),
        age: $('#age').val(),
    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM