判斷陣列物件中是否包含某個物件,判斷2個物件是否相等
var arr =[ {name:'zhangsan'}, {name:'lisi'}, {name:'wangwu'} ]; var json1 = {name:"zhangsan"}; // 方法一:(相容ie8) console.log(JSON.stringify(arr).indexOf(JSON.stringify(json1))); // 方法二: (前提是匹配和被匹配的存在賦值關係) var json2 = arr[0]; var json3 = {name:'zhangsan'}; console.log('json2',arr.indexOf(json2)); // 存在 console.log('json3',arr.indexOf(json3)); //不存在
underscore 中isEqual的原始碼;
// Internal recursive comparison function for `isEqual`. var eq = function(a, b, aStack, bStack) { // Identical objects are equal. `0 === -0`, but they aren't identical. // See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal). if (a === b) return a !== 0 || 1 / a === 1 / b; // A strict comparison is necessary because `null == undefined`. if (a == null || b == null) return a === b; // Unwrap any wrapped objects. if (a instanceof _) a = a._wrapped; if (b instanceof _) b = b._wrapped; // Compare `[[Class]]` names. var className = toString.call(a); if (className !== toString.call(b)) return false; switch (className) { // Strings, numbers, regular expressions, dates, and booleans are compared by value. case '[object RegExp]': // RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i') case '[object String]': // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is // equivalent to `new String("5")`. return '' + a === '' + b; case '[object Number]': // `NaN`s are equivalent, but non-reflexive. // Object(NaN) is equivalent to NaN if (+a !== +a) return +b !== +b; // An `egal` comparison is performed for other numeric values. return +a === 0 ? 1 / +a === 1 / b : +a === +b; case '[object Date]': case '[object Boolean]': // Coerce dates and booleans to numeric primitive values. Dates are compared by their // millisecond representations. Note that invalid dates with millisecond representations // of `NaN` are not equivalent. return +a === +b; } if (typeof a != 'object' || typeof b != 'object') return false; // Assume equality for cyclic structures. The algorithm for detecting cyclic // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`. var length = aStack.length; while (length--) { // Linear search. Performance is inversely proportional to the number of // unique nested structures. if (aStack[length] === a) return bStack[length] === b; } // Objects with different constructors are not equivalent, but `Object`s // from different frames are. var aCtor = a.constructor, bCtor = b.constructor; if ( aCtor !== bCtor && // Handle Object.create(x) cases 'constructor' in a && 'constructor' in b && !(_.isFunction(aCtor) && aCtor instanceof aCtor && _.isFunction(bCtor) && bCtor instanceof bCtor) ) { return false; } // Add the first object to the stack of traversed objects. aStack.push(a); bStack.push(b); var size, result; // Recursively compare objects and arrays. if (className === '[object Array]') { // Compare array lengths to determine if a deep comparison is necessary. size = a.length; result = size === b.length; if (result) { // Deep compare the contents, ignoring non-numeric properties. while (size--) { if (!(result = eq(a[size], b[size], aStack, bStack))) break; } } } else { // Deep compare objects. var keys = _.keys(a), key; size = keys.length; // Ensure that both objects contain the same number of properties before comparing deep equality. result = _.keys(b).length === size; if (result) { while (size--) { // Deep compare each member key = keys[size]; if (!(result = _.has(b, key) && eq(a[key], b[key], aStack, bStack))) break; } } } // Remove the first object from the stack of traversed objects. aStack.pop(); bStack.pop(); return result; }; // Perform a deep comparison to check if two objects are equal. _.isEqual = function(a, b) { return eq(a, b, [], []); };
相關推薦
判斷陣列物件中是否包含某個物件,判斷2個物件是否相等
var arr =[ {name:'zhangsan'}, {name:'lisi'}, {name:'wangwu'} ]; var json1 = {name:"zhangsan"}; // 方法一:(相容ie8) console.log(JSON.stri
js篇-判斷陣列物件中是否含有某個值,並返回該條資料
專案背景需求是: 已知: var a=[{name:'jenny',age:18},{name:'john',age:19},{name:'jack',age:20}] var b ='jenny' 返回: {name:'jenny',age:18} 解題思路:現將陣列轉成字串,判斷改陣列中是否存
在Java中如何高效判斷數組中是否包含某個元素
clas AC ray 而且 一個 nan 使用 一個數 component 如何檢查一個數組(無序)是否包含一個特定的值?這是一個在Java中經常用到的並且非常有用的操作。同時,這個問題在Stack Overflow中也是一個非常熱門的問題。在投票比較高的幾個答案中給出了
判斷陣列中是否包含某元素,判斷字串中是否包含某字串
let arr = [1, 2, 3, 4]; if (arr.indexOf('1') > -1) {} //判斷1是否在陣列arr中,true=是 let str = string; if (str.indexOf('t') !== -1) {}&nb
在java中判斷一個字串中是否包含某個字元或字串
一、contains方法 java.lang.String.contains() 方法返回true,當且僅當此字串包含指定的char值序列 此方法返回true,如果此字串包含,否則返回false。 public static void main(String[] args
js中判斷字串A中是否包含字串B,並將字串B替換為指定字串C
直接看例項: 上圖中buildType為:從後臺查詢的資料,其中dic為自定義註解,其中包含兩個值,code和name,在頁面上我們需要顯示name。 dic.name在未經過處理之前的值為:新址新建獨享站,新址新建共享站,共址共享等等,但是現在需要我們在顯示的時候不能顯
c#動態型別,及動態物件的建立,合併2個物件,map
經常會遇到這樣的情況,我們在響應客戶端請求的資料的時候需要對資料進行處理,比如資料庫中的資料是int型,它可能表示某個列舉,或者其它的邏輯意義(資料庫這樣的設計可能是從資料安全性、儲存量上等角度考慮),但是客戶端顯示的時候需要是它具體的意義。這個時候我們的處理方式一般都是2
判斷陣列中是否包含某個物件
要想比較需要重寫-isEqual:和-hash 當判斷anObject是否在當前的NSArray中的時候,是通過呼叫isEqual:這個方法來判斷的,isEqual:本質是在比較兩個物件的hash值 @interface Person @property NSStrin
Query判斷陣列中是否包含某個元素
JQuery判斷陣列中是否包含某個元素使用$.inArray("js", arr); var arr = [ "xml", "html", "css", "js" ]; $.in
Map.containsKey方法——判斷Map集合物件中是否包含指定的鍵名
containsKey方法用來判斷Map集合物件中是否包含指定的鍵名。 語法 boolean containsKey(Object key) 返回值:如果Map集合中包含指定的鍵名,則返回true;否則返回false。 引數:key是要查詢的Map集合的鍵名物件。
node js 判斷陣列中是否包含某個值
判斷陣列中是否包含某個值這裡有四種方法。 用的測試資料: let arr=["a","b","c"]; let arr2={"a":"aaa","b":"bbb","c":"ccc"}; in判斷是否在陣列的key裡 in操作符針對的是key,而非value。而對於普通的一
js 判斷字串是否包含某字串,String物件中查詢子字元indexOf,查詢字串出現的次數split
varCts = "bblText"; if(Cts.indexOf("Text") > 0 ) { alert('Cts中包含Text字串'); } indexOf用法: 返回 String 物件內第一次出現子字串的字元位置。 strObj.
Java中高效判斷陣列中是否包含某個元素。
如何檢查一個數組(無序的)是否包含一個特定的值呢?這是一個在Java中經常用到的並且非常有用的操作。本文將分析幾種常見用法及其時間成本。1、使用 ava.util.Arrays.asList(arr)
Java中如何高效的判斷陣列中是否包含某個元素
如何檢查一個數組(無序)是否包含一個特定的值?這是一個在Java中經常用到的並且非常有用的操作。同時,這個問題在Stack Overflow中也是一個非常熱門的問題。在投票比較高的幾個答案中給出了幾種不同的方法,但是他們的時間複雜度也是各不相同的。本文將分析幾種常見用法
java中如何高效的判斷陣列中是否包含某個元素---
package zaLearnpackage; import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; import java.util.HashSet; import java.uti
在Java中如何高效判斷陣列中是否包含某個元素
如何檢查一個數組(無序)是否包含一個特定的值?這是一個在Java中經常用到的並且非常有用的操作。同時,這個問題在Stack Overflow中也是一個非常熱門的問題。在投票比較高的幾個答案中給出了幾種不同的方法,但是他們的時間複雜度也是各不相同的。本文將分析
js判斷json物件中是否含有某個屬性
for(var item in data.resultObject) { if(!data.resultObject[item].hasOwnProperty("startTime")){
java中如何高效判斷陣列中是否包含某個特定的值
四種不同方式檢查陣列是否包含某個值 使用List: public static boolean useList(String[] arr, String targetValue) {
JS—使用indexOf()來判斷字串中是否包含某個值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js判斷陣列中是否包含一個數字</title> <scri
thymeleaf和freemarker從物件中獲取時間並格式化,同時非空判斷
thymeleaf,freemarker從物件中獲取時間並格式化,同時非空判斷 freemarker物件非空判斷:${(creditInformationDO.delFlag)!} freemarker從物件中獲取時間並格式化:${(creditInformationD