如何计算JSON中元素为空的数量?

时间:2018-08-15 14:58:33

标签: jquery json

我有一个像这样的json数据:

{
    "contract_no":"1",
    "history_no":"",
    "f_sin":"",
    "line_no":"",
    "p_typeid":"3",
    "compare_typeid":1,
    "val_typeid":"1",
    "goal_val":0,
    "count_typeid":1,
    "f_pb_ignore":0,
    "all_products":0,
    "judge_typeid":"",
    "reach_condition_array":[
        {
            "up_s_line_no":1,
            "t_from":111,
            "t_to":222,
            "rebate":11
        },
        {
            "up_s_line_no":2,
            "t_from":222,
            "t_to":null,
            "rebate":22
        }
    ],
    "series_appoint":[
        {
            "down_s_line_no":1,
            "deptCD":"0",
            "categoryCD":"12",
            "singleJan":"",
            "seriesJan":"",
            "fExclude":0
        },
{
            "down_s_line_no":1,
            "deptCD":null,
            "categoryCD":"4",
            "singleJan":"",
            "seriesJan":"",
            "fExclude":0
        },
{
            "down_s_line_no":1,
            "deptCD":"55",
            "categoryCD":"",
            "singleJan":"",
            "seriesJan":"",
            "fExclude":0
        },
{
            "down_s_line_no":1,
            "deptCD":"",
            "categoryCD":2222,
            "singleJan":"58796",
            "seriesJan":"1000000009",
            "fExclude":0
        }
    ]
}

我想知道数组“ series_appoint”中有多少个“ deptCD”元素为“”或null,以及有多少“ singleJan”是“”或null。

在我的示例中,“ deptCD”的计数为“”或null为2,“ singleJan”的计数为“”或null为3。

1 个答案:

答案 0 :(得分:1)

您可以使用listData.map(data -> data.getAttribute) 从数组中返回匹配的元素,然后计算长度。

Array#filter