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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?