如何使用JS筛选JSON数据


JSON数据如下

{
    status: "ok",
    image_size_list: [
    {
        image_size_id: "22",
        ad_class: "N"
    },
    {
        image_size_id: "19",
        ad_class: "C"
    },
    {
        image_size_id: "24",
        ad_class: "N"
    },
    {
        image_size_id: "15",
        ad_class: "C"
    }]
}

我想得到一个新的JSON,里面只有ad_class: "N"的数据,而没有ad_class: "C"的数据
可以使用jQuery,或者直接写原生JS

jquery json JavaScript

划水的兔先生 10 years, 10 months ago

$.grep看上去比较自然。

var data = {
    status: "ok",
    image_size_list: [
    {
        image_size_id: "22",
        ad_class: "N"
    },
    {
        image_size_id: "19",
        ad_class: "C"
    },
    {
        image_size_id: "24",
        ad_class: "N"
    },
    {
        image_size_id: "15",
        ad_class: "C"
    }]
};
data.image_size_list = $.grep(data.image_size_list,function(item){ 
    return item.ad_class=='N';
});

该函数的说明: http://manual.pandaidea.com/jQuery/#p...

hecate answered 10 years, 10 months ago

Your Answer