6

重複の可能性:
Javascript 配列 (JSON 形式) を動的に作成するにはどうすればよいですか?

私は次のものを作成しようとしています:

var employees = {"accounting": [   // accounting is an array in employees.
                    { "firstName" : "John",  // First element
                      "lastName"  : "Doe",
                      "age"       : 23 },

                    { "firstName" : "Mary",  // Second Element
                      "lastName"  : "Smith",
                      "age"       : 32 }
                  ] // End "accounting" array.                                  

    } // End Employees

私は始めました:

 var employees=new Array();

配列に動的に追加し続けるにはどうすればよいですか (変数で firstName を変更する可能性があります)。

4

4 に答える 4

7
var employees = {accounting: []};

employees.accounting.push({
    "firstName" : "New",
    "lastName"  : "Employee",
    "age"       : 18
});
于 2012-08-22T06:08:15.807 に答える
2
employees.accounting.push({ "firstName" : "New",  // First element
                      "lastName"  : "Person",
                      "age"       : 55 });
于 2012-08-22T06:08:58.940 に答える
0
function Employee(firstName, lastName, age) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
}

var employees = {};
employees.accounting = [];
employees.accounting.push(new Employee('John', 'Doe', 23));
employees.accounting.push(new Employee('Mary', 'Smith', 32));
于 2012-08-22T06:12:22.730 に答える
0
var urNewFirstName='new john';
employees.accounting[0].firstName = urNewFistName;
于 2012-08-22T06:11:33.217 に答える