以js的数组举例:
1 var a = [‘1‘]; 2 a[5] = 52; 3 a.length //这儿的结果是6,也就是说,中间的key会自动补全,而值呢,是undefined
一,添加和删除
1,一维数组
1 test = {}; //空json对像 2 test[‘firstname‘] = "tank"; //添加二个元素 3 test[‘lastname‘] = "zhang"; 4 console.log(test); //查看 5 6 delete test[‘lastname‘]; //删除json中的某个元素 7 console.log(test);
2,二维数组
1 test1 = [{"name":"tank","total":"100"},{"name":"zhang","total":"23"},{"name":"hao","total":"325"}]; 2 add = {"name":"may"}; 3 4 test1.push(add); //添加一个元素 5 console.log(test1); 6 7 delete test1[2]; //删除一个元素 8 console.log(test1);
二,排序
1,一维数组
1 test = ["100","23","325"]; //定义个数组 2 3 function sortNumber(a,b) //定义排序方法 4 { 5 return a - b 6 } 7 8 test1_sort=test.sort(sortNumber); 9 10 console.log(test1_sort);
2,二维数组
参考地址:http://blog.51yip.com/jsjquery/1583.html