douyun1852 2014-05-28 00:26
浏览 169
已采纳

如何删除json元素?

I'am using dataTables plugin for my table, and it has an json data from the database like :

{"sEcho":0,
"iTotalRecords":1,
"iTotalDisplayRecords":1,
"aaData":[["contentFieldA","contentFieldB","contentFieldC"],
          ["contentFieldA","contentFieldB","contentFieldC"]],
"sColumns":"fieldA,fieldB,fieldC"}

for some reason I need to remove the "aaData" and "sColumns" value on the first index (so "contentFieldA" and "fieldA" should be removed). and the json data will be :

{"sEcho":0,
 "iTotalRecords":1,
 "iTotalDisplayRecords":1,
 "aaData":[["contentFieldB","contentFieldC"],
           ["contentFieldB","contentFieldC"]],
 "sColumns":"fieldB,fieldC"}

Can anyone help me.. Thanks

  • 写回答

2条回答 默认 最新

  • doulangpeng3933 2014-05-28 06:41
    关注

    If you have :

    var test = {"sEcho":0,
    "iTotalRecords":1,
    "iTotalDisplayRecords":1,
    "aaData":[["contentFieldA","contentFieldB","contentFieldC"],
              ["contentFieldA","contentFieldB","contentFieldC"]],
    "sColumns":"fieldA,fieldB,fieldC"};
    

    aaData is an array of arrays, so remove first index of each subarray like this :

    for (var i=0;i<test['aaData'].length;i++) {
        test['aaData'][i].splice(0,1);
    }
    

    deleting first index from sColumns is a little bit harder, since it is a string :

    test['sColumns']=test['sColumns'].split(',').splice(1,2).join();
    

    Notice that splice(1,2) is hardcoded, if you have 4 columns it should be splice(1,3), 5 columns splice(1,4) and so on.

    Running those two cleanups will give the desired result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法