jquery grep on json object array

北战南征 提交于 2019-11-30 20:25:02

You will need to use both grep and map. If a is the array described above (but with name1, name2, etc), then after the following:

var b = $.grep(a, function(el, i) {
    return el.location1.toLowerCase() === "yes" 
           || el.location2.toLowerCase() === "yes" 
           || el.location3.toLowerCase() === "yes" 
           || el.location4.toLowerCase() === "yes" 
           || el.location5.toLowerCase() === "yes";
});

var c = $.map(b, function(el, i) {
    return {
        name: el.name,
        description: el.description
    };
});

c will contain [{"name":"name1","description":"description of services1"},{"name":"name2","description":"description of services2"}]

See example →

My version is very similar to previous answer, I hope it helps:

    var checkYes = function(element) {

        var isYesInside = false;

        $.each(element, function(key, value) {
            if (value == "yes")
                isYesInside = true;
        });

        return isYesInside;
    };

    var yeses = $.grep(a, function(element, index) {
        return checkYes(element);
    });

    var finalArray = $.map(yeses, function(el, i) {
        return {
            name: el.name,
            description: el.description
        };
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!