关于json中对象的删除

 1 一个json对象在后台产生了,但是有些数据可能无效或者不合法,所以需要在前台作些例外处理,比如删除掉。
 2  
 3 json的删除有很多种,直接用过 delete json对象方式。
 4  
 5 举例如下
 6 Js代码  
 7 var columns = [    
 8       {name:"bigTitle",caption:reportData.bigTitle,children:[  
 9     {name:"orgName",caption:"组织结构名称",width:100,mode:"string"},                                                     
10     {name:"helpinfo",caption:reportData.columnCaption[0],  
11      children:[{name:"workday_month",caption:"工作日数",width:80,mode:"number",format:"#"},  
12                {name:"loggedday_month",caption:"登录天数",width:80,mode:"number",format:"#"}]  
13     },  
14     {name:"helpinfo",caption:reportData.columnCaption[1],  
15      children:[{name:"workday_week1",caption:"工作日数",width:70,mode:"number",format:"#"},  
16                {name:"loggedday_week1",caption:"登录天数",width:70,mode:"number",format:"#"}]  
17     },  
18     {name:"helpinfo",caption:reportData.columnCaption[2],  
19      children:[{name:"workday_week2",caption:"工作日数",width:70,mode:"number",format:"#"},  
20                {name:"loggedday_week2",caption:"登录天数",width:70,mode:"number",format:"#"}]  
21     },   
22     {name:"helpinfo",caption:reportData.columnCaption[3],  
23      children:[{name:"workday_week3",caption:"工作日数",width:70,mode:"number",format:"#"},  
24                {name:"loggedday_week3",caption:"登录天数",width:70,mode:"number",format:"#"}]  
25     },   
26     {name:"helpinfo",caption:reportData.columnCaption[4],  
27      children:[{name:"workday_week4",caption:"工作日数",width:70,mode:"number",format:"#"},  
28                {name:"loggedday_week4",caption:"登录天数",width:70,mode:"number",format:"#"}]  
29     },   
30     {name:"helpinfo",caption:reportData.columnCaption[5],  
31      children:[{name:"workday_week5",caption:"工作日数",width:70,mode:"number",format:"#"},  
32                {name:"loggedday_week5",caption:"登录天数",width:70,mode:"number",format:"#"}]  
33     }  
34       ]}     
35      ];  
36  
37 根据rtData.columnCaption 判断,如果为null,则删除该节点,不予显示。
38  
39 用过 delete columns[0]['children'][6];
40  
41 无效,报js错误,后来发现 删除确实是删除了,但是最后还遗留了一个逗号,导致IE下报错,查了很多资料无解。
42  
43 最后换了个解决方法。
44  
45 columns[0]['children'].pop();
46  
47 pop()方法表示删除最后一个节点。
48  
49 工作日历的要求正好满足该要求,从后递减。
50  
51 其他类似的方法有如下:
52 var person={name:"yaoMing",sex:"m",age:"26"};
53 jsonObj2.persons.push(person);//数组最后加一条记录
54 jsonObj2.persons.pop();//删除最后一项
55 jsonObj2.persons.shift();//删除第一项
56 jsonObj2.persons.unshift(person);//数组最前面加一条记录
57 只要适合Javascript的方法都是可以用在JSON对象的数组中的!所以还有另外的方法splice( )进行crud操作!
58 //删除
59 jsonObj2.persons.splice(0,1);//开始位置,删除个数
60 //替换不删除
61 var self={name:"tom",sex:"m",age:"24"};
62 var brother={name:"Mike",sex:"m",age:"29"};
63 jsonObj2.persons.splice(1,0,self,brother);//开始位置,删除个数,插入对象
64 //替换并删除
65 var self={name:"tom",sex:"m",age:"24"};
66 var brother={name:"Mike",sex:"m",age:"29"};
67 jsonObj2.persons.splice(0,1,self,brother);//开始位置,删除个数,插入对象

 

posted @ 2015-07-17 11:58  闻杰  阅读(1403)  评论(0编辑  收藏  举报