關於json對象的刪除

來源:互聯網
上載者:User

標籤:blog   http   java   io   資料   for   ar   代碼   

摘自 http://www.cnblogs.com/wuyifu/p/3424621.html

一個json對象在後台產生了,但是有些資料可能無效或者不合法,所以需要在前台作些例外處理,比如刪除掉。
 
json的刪除有很多種,直接用過 delete json對象方式。
 
舉例如下
Js代碼 
var columns = [   
      {name:"bigTitle",caption:reportData.bigTitle,children:[ 
    {name:"orgName",caption:"組織圖名稱",width:100,mode:"string"},                                                    
    {name:"helpinfo",caption:reportData.columnCaption[0], 
     children:[{name:"workday_month",caption:"工作日數",width:80,mode:"number",format:"#"}, 
               {name:"loggedday_month",caption:"登入天數",width:80,mode:"number",format:"#"}] 
    }, 
    {name:"helpinfo",caption:reportData.columnCaption[1], 
     children:[{name:"workday_week1",caption:"工作日數",width:70,mode:"number",format:"#"}, 
               {name:"loggedday_week1",caption:"登入天數",width:70,mode:"number",format:"#"}] 
    }, 
    {name:"helpinfo",caption:reportData.columnCaption[2], 
     children:[{name:"workday_week2",caption:"工作日數",width:70,mode:"number",format:"#"}, 
               {name:"loggedday_week2",caption:"登入天數",width:70,mode:"number",format:"#"}] 
    },  
    {name:"helpinfo",caption:reportData.columnCaption[3], 
     children:[{name:"workday_week3",caption:"工作日數",width:70,mode:"number",format:"#"}, 
               {name:"loggedday_week3",caption:"登入天數",width:70,mode:"number",format:"#"}] 
    },  
    {name:"helpinfo",caption:reportData.columnCaption[4], 
     children:[{name:"workday_week4",caption:"工作日數",width:70,mode:"number",format:"#"}, 
               {name:"loggedday_week4",caption:"登入天數",width:70,mode:"number",format:"#"}] 
    },  
    {name:"helpinfo",caption:reportData.columnCaption[5], 
     children:[{name:"workday_week5",caption:"工作日數",width:70,mode:"number",format:"#"}, 
               {name:"loggedday_week5",caption:"登入天數",width:70,mode:"number",format:"#"}] 
    } 
      ]}    
     ]; 
 
根據rtData.columnCaption 判斷,如果為null,則刪除該節點,不予顯示。
 
用過 delete columns[0][‘children‘][6];
 
無效,報js錯誤,後來發現 刪除確實是刪除了,但是最後還遺留了一個逗號,導致IE下報錯,查了很多資料無解。
 
最後換了個解決方案。
 
columns[0][‘children‘].pop();
 
pop()方法表示刪除最後一個節點。
 
工作日曆的要求正好滿足該要求,從後遞減。
 
其他類似的方法有如下:
var person={name:"yaoMing",sex:"m",age:"26"};
jsonObj2.persons.push(person);//數組最後加一條記錄
jsonObj2.persons.pop();//刪除最後一項
jsonObj2.persons.shift();//刪除第一項
jsonObj2.persons.unshift(person);//數組最前面加一條記錄
只要適合Javascript的方法都是可以用在JSON對象的數組中的!所以還有另外的方法splice( )進行crud操作!
//刪除
jsonObj2.persons.splice(0,1);//開始位置,刪除個數
//替換不刪除
var self={name:"tom",sex:"m",age:"24"};
var brother={name:"Mike",sex:"m",age:"29"};
jsonObj2.persons.splice(1,0,self,brother);//開始位置,刪除個數,插入對象
//替換並刪除
var self={name:"tom",sex:"m",age:"24"};
var brother={name:"Mike",sex:"m",age:"29"};
jsonObj2.persons.splice(0,1,self,brother);//開始位置,刪除個數,插入對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.