第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

比較兩個JSON對象,如果鍵和值使用Javascript匹配,則返回true

比較兩個JSON對象,如果鍵和值使用Javascript匹配,則返回true

元芳怎么了 2021-05-11 21:17:02
我有兩個JSON對象,如果鍵和值匹配,則想返回'true'值。d1和d2的所有三個值都應(yīng)匹配。var d1 = [{"deviceid":"867874031097770","simno":"232ff33","slot":"1"},{"deviceid":"86787403100","simno":"ss343433","slot":"2"}];var d2 = {"deviceid":"867874031097770","simno":"232ff33","slot":"1"};我嘗試使用以下代碼,但不適用于JSON值數(shù)組。function equals ( 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 ( !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;}
查看完整描述

3 回答

?
慕尼黑8549860

TA貢獻1818條經(jīng)驗 獲得超11個贊

您可以d2對中的每個條目進行分類和比較d1。只需通過提供一個replacer函數(shù)作為中的第二個參數(shù)來確保在對對象進行字符串化時保持順序JSON.stringify。


非數(shù)組對象的屬性不能保證以任何特定順序進行字符串化。不要依賴于字符串化中同一對象內(nèi)屬性的順序。


function replacer(obj) {

  return Object.keys(obj).sort();

}


var d1 = [{"deviceid":"867874031097770", "simno":"ss343433", "slot":"1"}, 

          {"deviceid":"867874031097770","simno":"ss343433","slot":"1"}];


var d2 = {"deviceid":"867874031097770","slot":"1", "simno":"ss343433"};


function equals(searchArr, objToCheck) {

  var allEqual = true;

  for (index in searchArr) {

    const item = searchArr[index];

    if (JSON.stringify(item, replacer(item)) !== JSON.stringify(objToCheck, replacer(objToCheck))) {

      (objToCheck)));

      allEqual = false;

      break;

    }

  }

  return allEqual;

}


if (equals(d1, d2)) {

  console.log('All values of properties of d2 match with all entries in d1')

} else {

  console.log('d2 values do not match with all entries in d1');

}


查看完整回答
反對 回復(fù) 2021-05-13
  • 3 回答
  • 0 關(guān)注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號