javascript如何定义对象数组
编程学习 2021-07-04 19:59www.dzhlxh.cn编程入门
本文主要介绍javascript如何定义对象数组,两种方法,比较实用,希望能给大家做一个参考。
问题如下,已经完成单个对象的简单应用,希望定义一个数组,能包含多个student。
代码如下:
var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
var json = JSON.stringify(student);
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);
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);
student.name = "Lanny";
student.age = "25";
student.location = "China";
students = (students || []).push(student);
var json = JSON.stringify(students);
查看更多JavaScript的语法,大家可以关注:《》、《》,也希望大家多多支持狼蚁SEO。