[英]How to see if given object matches one of the objects returned from firebase
我試圖測試給定的object
匹配存儲並從firebase返回的stored objects
。 如果是,則返回true或在變量中存儲true。
以下是給定對象和firebase返回數據的示例
給定對象:
{name: "John", age: "32"}
存儲對象:
{
'-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 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"};
這是你如何檢查對象是否相等的方法。 你只需要循環對象
Javascript方式
for(key in storedObject){
if(JSON.stringify(storedObject[key]) === JSON.stringify(givenObject)){
alert('equal'+givenObject['name'] +givenObject['age']);
}
};
jQuery方式
使用$.each()
函數
$.each(storedObject,function(key,value){
if(JSON.stringify(value) === JSON.stringify(givenObject)){
alert('equal'+givenObject['name'] +givenObject['age']);
}
});
查看FIDDLE LINK另外,有關您的信息,請查看JavaScript LINK中的此SO 對象比較
為了完整起見,這是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);
}];
這里的概念是我們使用廣泛的查詢來引入我們需要的一系列數據,在這種情況下,所有名為John的用戶,然后在代碼中為所有返回的32歲的John用戶過濾該數據。
請注意,如果有多個John年齡為32歲,則結果數組將包含所有這些數據。 應該向NSPredicate提供更詳細的信息,以獲得您正在尋找的確切John(即SSN = x或駕駛執照= y)
以下是使用可重用代碼(需要符合ES5環境)的通用示例,因此它不僅限於您提供的數據。
// 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.