jquery grep json 对象数组

发布于 2024-11-14 21:13:17 字数 588 浏览 1 评论 0原文

我尝试使用 grep 过滤 json 对象数组,以便搜索该数组,如果键 #2-6 中任何一个的值为 yes,则返回键 1 和 7 的值。

该数组如下 - 换句话说,如果“位置”键的任何值为“是”,则名称和描述将作为列表项返回。

非常感谢任何帮助。

[
    {
        "name": "name",
        "location1": "no",
    "location2": "no",
    "location3": "yes",
    "location4": "no",
    "location5": "no",
    "description": "description of services"
    },

    {   
    "name": "name",
        "location1": "yes",
    "location2": "no",
    "location3": "yes",
    "location4": "no",
    "location5": "no",
    "description": "description of services"        
    }
]

Im trying to use grep to filter a json object array so that the array is searched and if the value of any of keys #2-6 are yes, the value of keys 1 and 7 are returned.

The array is below -- in other words, if any of values for the 'location' keys are yes, the name and description are returned as list items.

Any help is VERY much appreciated.

[
    {
        "name": "name",
        "location1": "no",
    "location2": "no",
    "location3": "yes",
    "location4": "no",
    "location5": "no",
    "description": "description of services"
    },

    {   
    "name": "name",
        "location1": "yes",
    "location2": "no",
    "location3": "yes",
    "location4": "no",
    "location5": "no",
    "description": "description of services"        
    }
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤者何惧 2024-11-21 21:13:17

您需要同时使用 grepmap。如果 a 是上述数组(但具有 name1name2 等),则在以下内容之后:

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 将包含 [{"name":"name1","description":"服务描述 1"},{"name":"name2","description":"服务描述 2"}]

<一href="http://jsfiddle.net/7zHP3/" rel="noreferrer">参见示例→

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 →

似最初 2024-11-21 21:13:17

我的版本与之前的答案非常相似,希望对您有所帮助:

    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
        };
    });

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
        };
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文