最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Jquery or Javascript check if object exists in collection of Json Objects - Stack Overflow

programmeradmin1浏览0评论

Given a json string like this:

[{"id":28,"Title":"Sweden"},{"id":56,"Title":"USA"},{"id":89,"Title":"England"}]

I need to check if an object exists, checking all fields, meaning:

{"id":28,"Title":"Sweden"} => exists
{"id":29,"Title":"Sweden"} => doesn't exist

OR

{"id":28,"Title":"Sweden"} => exists
{"id":28,"Title":"Sweden2"} => doesn't exist

The collection may have any number of objects and objects will always have an equal number of properties (id, title) or (id, title, firstName) etc.

Also, in order to check for existing object, does the string need to be parsed into a json object collection?

I tried this:

$.map(val, function (obj) {
    if (obj === val)
        alert('in');
    return obj; // or return obj.name, whatever.
});

Given a json string like this:

[{"id":28,"Title":"Sweden"},{"id":56,"Title":"USA"},{"id":89,"Title":"England"}]

I need to check if an object exists, checking all fields, meaning:

{"id":28,"Title":"Sweden"} => exists
{"id":29,"Title":"Sweden"} => doesn't exist

OR

{"id":28,"Title":"Sweden"} => exists
{"id":28,"Title":"Sweden2"} => doesn't exist

The collection may have any number of objects and objects will always have an equal number of properties (id, title) or (id, title, firstName) etc.

Also, in order to check for existing object, does the string need to be parsed into a json object collection?

I tried this:

$.map(val, function (obj) {
    if (obj === val)
        alert('in');
    return obj; // or return obj.name, whatever.
});
Share Improve this question edited Mar 28, 2013 at 13:31 Michael Celey 12.7k6 gold badges60 silver badges63 bronze badges asked Feb 28, 2013 at 8:57 ShaneKmShaneKm 21.4k46 gold badges176 silver badges307 bronze badges 2
  • +1 for Sweden. I <3 Sweden. – L0j1k Commented Feb 28, 2013 at 8:59
  • 1 You made a least little attempt to solve it yourself, right? ;) – dfsq Commented Feb 28, 2013 at 9:07
Add a ment  | 

3 Answers 3

Reset to default 2

Maybe something like this?

    function exists(obj, objs)
    {
        var objStr = JSON.stringify(obj);

        for(var i=0;i<objs.length; i++)
        {
            if(JSON.stringify(objs[i]) == objStr)
            {
                return 1;
            }
        }

        return 0;
    }


    /** some tests **/
    var x = [{"id":28,"Title":"Sweden"},{"id":56,"Title":"USA"},{"id":89,"Title":"England"}];
    var has = {"id":28,"Title":"Sweden"};
    var not = {"id":28,"Title":"Sweden2"};

    /* alerts yes */
    if(exists(has, x)) alert('yes');
    else alert('no');

    /* alerts no */
    if(exists(not, x)) alert('yes');
    else alert('no');

http://jsfiddle/zdhyf/

assuming you need a general solution, and not for only the example, you can checkout lodash ( http://lodash. ) which is a performant library for such operations, and specifically the isEqual function ( http://lodash./docs#isEqual ).

Alternatively, you can extend the above code to use arbitrary values to check against.

If you sure that properties of the objects in collection are always in the same order as in the object you are looking for, you can search for a substring instead of checking presence of the object:

var json = '[{"id":28,"Title":"Sweden"},{"id":56,"Title":"USA"},{"id":89,"Title":"England"}]',
obj1 = {"id":28,"Title":"Sweden"},
obj2 = {"id":29,"Title":"Sweden"};

alert(json.indexOf(JSON.stringify(obj1)) >= 0 ? 'Yes' : 'No'); // Yes
alert(json.indexOf(JSON.stringify(obj2)) >= 0 ? 'Yes' : 'No'); // No
发布评论

评论列表(0)

  1. 暂无评论