2

I am trying to test if the given object matches the stored objects that are stored and returned from firebase. If it does then return true or store true in a variable.

Here is the example of given object and firebase returned data

given object:

{name: "John", age: "32"}

stored object:

{ 
    '-JrYqLGTNLfa1zEkTG5J': { name: "John", age: "32" },
    '-JrkhWMKHhrShX66dSWJ': { name: "Steve", age: "25" },
    '-JrkhtHQPKRpNh0B0LqJ': { name: "Kelly", age: "33" },
    '-JrkiItisvMJZP1fKsS8': { name: "Mike", age: "28" },
    '-JrkiPqA8KyAMj2R7A2h': { name: "Liz", age: "22" } 
}
Abhinav
  • 8,028
  • 12
  • 48
  • 89
Chipe
  • 4,641
  • 10
  • 36
  • 64
  • Does your object has an ID attribute? How many stored objects do you have? – vlio20 Jun 14 '15 at 07:07
  • The object that is given wont have the unique id that is returned with the data from firebase. The stored object from firebase will grow so it may not be the same every time the test is checked – Chipe Jun 14 '15 at 07:12
  • The question asks if one object is equal to another object. I believe what you are really asking is how to find an object that has John and 32 as values within a list of other objects. The distinction can be highlighted in this case: what if the list of stored objects has more than one object with John and 32 as values? Which one is 'the same object'? – Jay Jun 14 '15 at 12:38

3 Answers3

5
var storedObject ={ 
    '-JrYqLGTNLfa1zEkTG5J': { name: "John", age: "32" },
    '-JrkhWMKHhrShX66dSWJ': { name: "Steve", age: "25" },
    '-JrkhtHQPKRpNh0B0LqJ': { name: "Kelly", age: "33" },
    '-JrkiItisvMJZP1fKsS8': { name: "Mike", age: "28" },
    '-JrkiPqA8KyAMj2R7A2h': { name: "Liz", age: "22" } 
};
var givenObject = {name: "John", age: "32"};

This is how you can check if objects are equal. You just have to loop the object

Javascript Way

for(key in storedObject){
    if(JSON.stringify(storedObject[key]) === JSON.stringify(givenObject)){
     alert('equal'+givenObject['name'] +givenObject['age']);
    }
};

jQuery Way

using $.each() function

$.each(storedObject,function(key,value){
    if(JSON.stringify(value) === JSON.stringify(givenObject)){
     alert('equal'+givenObject['name'] +givenObject['age']);
    }
});

Take a look at the FIDDLE LINK Also For your info, check out this SO Object comparison in JavaScript LINK

Community
  • 1
  • 1
Abhinav
  • 8,028
  • 12
  • 48
  • 89
2

For completeness here is a solution in iOS/MacOS

//query for all users
FQuery *allUsersRef = [usersRef queryOrderedByChild:@"name"];

//filter that query with a query for users named "John"
FQuery *justJohnRef = [allUsersRef queryEqualToValue:@"John"]; 

//read in all of the resulting users named John
[justJohnRef observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

    NSArray *johnArray = [snapshot.value allObjects];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age == %@", @"32"];
    NSArray *result = [johnArray filteredArrayUsingPredicate:predicate];

    NSLog(@"%@", result);

}];

The concept here is that we use a broad query to bring in a range of data that we need, in this case all users named John, then filter that data in code for all of the John users returned that are age 32.

Note that if there are more than one John age 32 the result array will contain all of them. More detailed info should be supplied to the NSPredicate to get the exact John you are looking for (i.e. SSN = x or Drivers License = y)

Jay
  • 34,438
  • 18
  • 52
  • 81
0

Here is a generic example using reusable code (ES5 compliant environment required), so it is not limited to just the data that you have provided.

// some generic reuseable code
(function () {
    'use strict';

    function $isPrimitive(inputArg) {
        var type = typeof inputArg;

        return type === 'undefined' || inputArg === null || type === 'boolean' || type === 'string' || type === 'number' || type === 'symbol';
    }

    function $isFunction(inputArg) {
        return typeof inputArg === 'function';
    }

    function $isDate(inputArg) {
        return Object.prototype.toString.call(inputArg) === '[object Date]';
    }

    function $isRegExp(inputArg) {
        return Object.prototype.toString.call(inputArg) === '[object RegExp]';
    }

    function $isString(inputArg) {
        return Object.prototype.toString.call(inputArg) === '[object String]';
    }

    function $isArguments(inputArg) {
        return Object.prototype.toString.call(inputArg) === '[object Arguments]';
    }

    function $getItem(object, index) {
        var item;

        if ($isString(object)) {
            item = object.charAt(index);
        } else {
            item = object[index];
        }

        return item;
    }

    var de = function (a, b, circ) {
        if (a === b) {
            return true;
        }

        var aType,
            bType,
            aIsArgs,
            bIsArgs,
            aIsPrim,
            bIsPrim,
            aCirc,
            bCirc,
            ka,
            kb,
            length,
            index,
            it;

        if ($isDate(a) && $isDate(b)) {
            return a.getTime() === b.getTime();
        }

        if ($isRegExp(a) && $isRegExp(b)) {
            return a.source === b.source &&
                a.global === b.global &&
                a.multiline === b.multiline &&
                a.lastIndex === b.lastIndex &&
                a.ignoreCase === b.ignoreCase &&
                a.sticky === b.sticky;
        }

        aIsPrim = $isPrimitive(a);
        bIsPrim = $isPrimitive(b);
        if ((aIsPrim || $isFunction(a)) && (bIsPrim || $isFunction(b))) {
            /*jslint eqeq: true */
            return a == b;
        }

        if (aIsPrim || bIsPrim) {
            return a === b;
        }

        if (a.prototype !== b.prototype) {
            return false;
        }

        if (circ.a.indexOf(a) === -1) {
            circ.a.push(a);
        } else {
            aCirc = true;
        }

        if (circ.b.indexOf(b) === -1) {
            circ.b.push(b);
        } else {
            bCirc = true;
        }

        if (aCirc && bCirc) {
            circ.cnt += 1;
        } else {
            circ.cnt = 0;
        }

        if (circ.cnt > 200) {
            throw new RangeError('Circular reference limit exceeded');
        }

        aIsArgs = $isArguments(a);
        bIsArgs = $isArguments(b);
        if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs)) {
            return false;
        }

        if (aIsArgs) {
            return de(Array.prototype.slice.call(a), Array.prototype.slice.call(b), circ);
        }

        ka = Object.keys(a);
        kb = Object.keys(b);
        length = ka.length;
        if (length !== kb.length) {
            if (Array.isArray(a) && Array.isArray(b)) {
                if (a.length !== b.length) {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            ka.sort();
            kb.sort();
            for (index = 0; index < length; index += 1) {
                if (ka[index] !== kb[index]) {
                    return false;
                }
            }
        }

        for (index = 0; index < length; index += 1) {
            it = ka[index];
            if (!de($getItem(a, it), $getItem(b, it), circ)) {
                return false;
            }
        }

        aType = typeof a;
        bType = typeof b;

        return aType === bType;
    };

    if (!Object.prototype.deepEqual) {
        Object.defineProperty(Object.prototype, 'deepEqual', {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (b) {
                var a = this;
                
                return de(a, b, {
                    a: [],
                    b: [],
                    cnt: 0
                });
            }
        });
    }

    if (!Object.prototype.forKeys) {
        Object.defineProperty(Object.prototype, 'forKeys', {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (fn, thisArg) {
                var object = Object(this),
                    keys,
                    length,
                    val,
                    index,
                    it;

                if (!$isFunction(fn)) {
                    throw new TypeError('Argument is not a function: ' + fn);
                }

                keys = Object.keys(object);
                length = keys.length;
                val = false;
                for (index = 0; index < length; index += 1) {
                    it = keys[index];
                    val = !!fn.call(thisArg, $getItem(object, it), it, object);
                    if (val) {
                        break;
                    }
                }

                return val;
            }
        });
    }
}());

// example of use with your data
var stored = {
        '-JrYqLGTNLfa1zEkTG5J': {
            name: "John",
            age: "32"
        },
            '-JrkhWMKHhrShX66dSWJ': {
            name: "Steve",
            age: "25"
        },
            '-JrkhtHQPKRpNh0B0LqJ': {
            name: "Kelly",
            age: "33"
        },
            '-JrkiItisvMJZP1fKsS8': {
            name: "Mike",
            age: "28"
        },
            '-JrkiPqA8KyAMj2R7A2h': {
            name: "Liz",
            age: "22"
        }
    },
    given = {
        name: "John",
        age: "32"
    },
    found = stored.forKeys(function (item) {
        return item.deepEqual(this);
    }, given);

document.getElementById('out').textContent = 'given was matched in stored: ' + found;
<pre id="out"></pre>
Xotic750
  • 22,914
  • 8
  • 57
  • 79