JS判斷兩個數(shù)組或?qū)ο笫欠裣嗤姆椒ㄊ纠?/h1>
更新時間:2019年02月28日 08:49:11 作者:huangpb0624
這篇文章主要介紹了JS判斷兩個數(shù)組或?qū)ο笫欠裣嗤姆椒?結(jié)合實例形式分析了javascript針對簡單數(shù)組與對象進行判斷的相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了JS判斷兩個數(shù)組或?qū)ο笫欠裣嗤姆椒ā7窒斫o大家供大家參考,具體如下:
JS 判斷兩個數(shù)組是否相同
要判斷2個數(shù)組是否相同,首先要把數(shù)組進行排序,然后轉(zhuǎn)換成字符串進行比較。
JSON.stringify([1,2,3].sort()) === JSON.stringify([3,2,1].sort()); //true
或者
[1,2,3].sort().toString() === [3,2,1].sort().toString(); //true
經(jīng)驗證,上述方法對復雜數(shù)組結(jié)構(gòu)不適用。
JS 判斷兩個對象是否相同
這是網(wǎng)上某大神封裝對比對象是否相同的 function。
let cmp = ( x, y ) => {
// If both x and y are null or undefined and exactly the same
if ( x === y ) {
return true;
}
// If they are not strictly equal, they both need to be Objects
if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) {
return false;
}
//They must have the exact same prototype chain,the closest we can do is
//test the constructor.
if ( x.constructor !== y.constructor ) {
return false;
}
for ( var p in x ) {
//Inherited properties were tested using x.constructor === y.constructor
if ( x.hasOwnProperty( p ) ) {
// Allows comparing x[ p ] and y[ p ] when set to undefined
if ( ! y.hasOwnProperty( p ) ) {
return false;
}
// If they have the same strict value or identity then they are equal
if ( x[ p ] === y[ p ] ) {
continue;
}
// Numbers, Strings, Functions, Booleans must be strictly equal
if ( typeof( x[ p ] ) !== "object" ) {
return false;
}
// Objects and Arrays must be tested recursively
if ( ! Object.equals( x[ p ], y[ p ] ) ) {
return false;
}
}
}
for ( p in y ) {
// allows x[ p ] to be set to undefined
if ( y.hasOwnProperty( p ) && ! x.hasOwnProperty( p ) ) {
return false;
}
}
return true;
};
經(jīng)檢測,同樣也不支持復雜數(shù)據(jù)結(jié)構(gòu)的對象。
一般情況下用的話上述2種方法已經(jīng)夠用了,拿來作比較的一般都是簡單的數(shù)據(jù)結(jié)構(gòu)。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《javascript面向?qū)ο笕腴T教程》、《JavaScript數(shù)學運算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設計有所幫助。
相關(guān)文章
-
寫了一個layout,拖動條連貫,內(nèi)容區(qū)可為iframe
寫了一個layout,拖動條連貫,內(nèi)容區(qū)可為iframe... 2007-08-08
最新評論
本文實例講述了JS判斷兩個數(shù)組或?qū)ο笫欠裣嗤姆椒ā7窒斫o大家供大家參考,具體如下:
JS 判斷兩個數(shù)組是否相同
要判斷2個數(shù)組是否相同,首先要把數(shù)組進行排序,然后轉(zhuǎn)換成字符串進行比較。
JSON.stringify([1,2,3].sort()) === JSON.stringify([3,2,1].sort()); //true
或者
[1,2,3].sort().toString() === [3,2,1].sort().toString(); //true
經(jīng)驗證,上述方法對復雜數(shù)組結(jié)構(gòu)不適用。
JS 判斷兩個對象是否相同
這是網(wǎng)上某大神封裝對比對象是否相同的 function。
let cmp = ( x, y ) => { // If both x and y are null or undefined and exactly the same if ( x === y ) { return true; } // If they are not strictly equal, they both need to be Objects if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) { return false; } //They must have the exact same prototype chain,the closest we can do is //test the constructor. if ( x.constructor !== y.constructor ) { return false; } for ( var p in x ) { //Inherited properties were tested using x.constructor === y.constructor if ( x.hasOwnProperty( p ) ) { // Allows comparing x[ p ] and y[ p ] when set to undefined if ( ! y.hasOwnProperty( p ) ) { return false; } // If they have the same strict value or identity then they are equal if ( x[ p ] === y[ p ] ) { continue; } // Numbers, Strings, Functions, Booleans must be strictly equal if ( typeof( x[ p ] ) !== "object" ) { return false; } // Objects and Arrays must be tested recursively if ( ! Object.equals( x[ p ], y[ p ] ) ) { return false; } } } for ( p in y ) { // allows x[ p ] to be set to undefined if ( y.hasOwnProperty( p ) && ! x.hasOwnProperty( p ) ) { return false; } } return true; };
經(jīng)檢測,同樣也不支持復雜數(shù)據(jù)結(jié)構(gòu)的對象。
一般情況下用的話上述2種方法已經(jīng)夠用了,拿來作比較的一般都是簡單的數(shù)據(jù)結(jié)構(gòu)。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《javascript面向?qū)ο笕腴T教程》、《JavaScript數(shù)學運算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設計有所幫助。
相關(guān)文章
寫了一個layout,拖動條連貫,內(nèi)容區(qū)可為iframe
寫了一個layout,拖動條連貫,內(nèi)容區(qū)可為iframe...2007-08-08