当然,因为它是一个表,所以拥有一个对象数组是有意义的。请注意,对象用花括号括起来,数组用括号括起来:
var myArray = []; // Initialize empty array
var myObject = {}; // Initialize empty object
这应该可以满足您的需求:
// Initialize variables
var newEntry, table = [];
// Create a new object
newEntry = {
id: '',
price: '',
description: ''
};
// Add the object to the end of the array
table.push(newEntry);
与此相同:
// Initialize array
var table = [];
// Create object and add the object to the end of the array
table.push({
id: '22',
price: '$222',
description: 'Foo'
});
您现在可以访问如下属性:table [0] .id; //‘22’
在现代浏览器上,如果您希望数据在各个会话(例如cookie)之间持久存在,则可以使用sessionStorage或localStorage对象。
当您要将数据发送到服务器时,将通过网络发送表的JSON版本:
var data = JSON.stringify(table);