当前位置:主页 > 网页前端 > JavaScript代码 >

javascript如何定义对象数组

时间:2020-11-29 11:26:44 | 栏目:JavaScript代码 | 点击:

问题如下,已经完成单个对象的简单应用,希望定义一个数组,能包含多个student。

复制代码 代码如下:
var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
var json = JSON.stringify(student);

方法一:

复制代码 代码如下:
var students = [];
students[students.length] = new Object();
students[students.length].name = "Lanny";
students[students.length].age = "25";
students[students.length].location = "China";
var json = JSON.stringify(students);

方法二:

复制代码 代码如下:
var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
students = (students || []).push(student);
var json = JSON.stringify(students);

您可能感兴趣的文章:

相关文章