weixin_33735077 2019-10-25 03:49 采纳率: 0%
浏览 470

在js中过滤json结果

filter data from result of json call

I am trying to do pre-processing JSON data before feed it to the dataTables. the reason is I need to separate the source data into 3 datatables. i can do it in the server-side in java but that approach is undesirable. Please don't ask why, it is just due to load balancing calculation.

 type: "GET",
            dataType: 'json',
            contentType: 'application/json',
            success: function (data) {
                console.log(data);
                var table = $('#tbl1');
                var tableUsul = $('#tbl1Terima');
                var tableTolak = $('#tbl2Tolak');


                table.DataTable({
                    "data": data.object, // i want to maipulate this
                    "processing": true,
                    "destroy": true,
                    "pagingType": "numbers",
                    "columns": [
                        {"data": null, "class": "rowCenter"},
                        {"data": "0"},
                        {"data": "1"},
                        {"data": "2"}
                    ........

console.log(data.object) result

[
[1,"myname1","1000"],
[0,"myname2","0"],
[1,"myname5","12121"],
[1,"myname2","23455"]
]

i want to filter data.object. so in the database, the 1st column consists of 1 and 0, I want to show only 1 or only 0. i was trying to use data.filter(function(){}) but the js does not recognize the function filter.

  • 写回答

2条回答 默认 最新

  • weixin_33674976 2019-10-25 04:11
    关注

    you can try something like below.

    var data = [
    [1,"myname1","1000"],
    [0,"myname2","0"],
    [1,"myname5","12121"],
    [1,"myname2","23455"]
    ];
    var newArray = [];
    for (i = 0; i < data.length; i++) {
    
     var  element = data[i]; 
    if(element[0] == 1)
    { 
       newArray.push(element);
    }
    
    }
    console.log(newArray)

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝