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

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

比較兩個對象屬性但不工作

比較兩個對象屬性但不工作

素胚勾勒不出你 2021-08-20 15:34:49
這是我的代碼。我知道這并不完全嚴格,但請說明為什么 let...in 在這里不能正常工作。const object1 = {here: 1, object: 3};const obj = {here: 1, object: 2};function comp(a, b) {  if (typeof a == typeof b) {    let arra = Object.keys(a);    let arrb = Object.keys(b);    for (let key in arra){      if (a[key] == b[key]) return true    }        return false  }}console.log(comp(obj, object1))以上打印,true但它應(yīng)該打印false
查看完整描述

3 回答

?
12345678_0001

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

你得到true,因為你return true 在你的for循環(huán),只要從一個對象的關(guān)鍵值等于鍵值對另一個對象。因此,當您的代碼看到該here屬性時,它將return true停止您的函數(shù)運行任何進一步的代碼。


您需要刪除此檢查,并且僅return false在您的 for 循環(huán)中,這樣您的 for 循環(huán)只會在它永不返回時完成(即:所有鍵值對都相等)。


此外,for..in循環(huán)將遍歷對象中的鍵,因此,無需將對象的鍵(使用Object.keys)放入數(shù)組中(因為這樣您將遍歷數(shù)組的鍵(即:索引))。


但是,話雖如此,您可以O(shè)bject.keys用來幫助解決另一個問題。您可以使用它來獲取兩個對象中的屬性數(shù)量,因為您知道如果兩個對象中的屬性數(shù)量不同,則它們是不相同的


請參閱下面的示例:


const object1 = {

  here: 1,

  object: 3

};

const obj = {

  here: 1,

  object: 3

};


function comp(a, b) {

  if (typeof a == typeof b) {

    if(Object.keys(a).length !== Object.keys(b).length) {

      return false; // return false (stop fruther code execution)

    }

  

    for (let key in a) { // loop through the properties of larger object (here I've chosen 'a') - no need for Object.keys

      if (a[key] != b[key]) 

        return false; // return false (stops any further code executing)

    }

    return true; // we only reach this point if the for loop never returned false

  }

  return false; // we reach this point when the two types don't match, and so we can say they're not equal

}


console.log(comp(obj, object1))


查看完整回答
反對 回復 2021-08-20
?
呼如林

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

你得到true,因為你return true 在你的for循環(huán),只要從一個對象的關(guān)鍵值等于鍵值對另一個對象。因此,當您的代碼看到該here屬性時,它將return true停止您的函數(shù)運行任何進一步的代碼。


您需要刪除此檢查,并且僅return false在您的 for 循環(huán)中,這樣您的 for 循環(huán)只會在它永不返回時完成(即:所有鍵值對都相等)。


此外,for..in循環(huán)將遍歷對象中的鍵,因此,無需將對象的鍵(使用Object.keys)放入數(shù)組中(因為這樣您將遍歷數(shù)組的鍵(即:索引))。


但是,話雖如此,您可以O(shè)bject.keys用來幫助解決另一個問題。您可以使用它來獲取兩個對象中的屬性數(shù)量,因為您知道如果兩個對象中的屬性數(shù)量不同,則它們是不相同的


請參閱下面的示例:


const object1 = {

  here: 1,

  object: 3

};

const obj = {

  here: 1,

  object: 3

};


function comp(a, b) {

  if (typeof a == typeof b) {

    if(Object.keys(a).length !== Object.keys(b).length) {

      return false; // return false (stop fruther code execution)

    }

  

    for (let key in a) { // loop through the properties of larger object (here I've chosen 'a') - no need for Object.keys

      if (a[key] != b[key]) 

        return false; // return false (stops any further code executing)

    }

    return true; // we only reach this point if the for loop never returned false

  }

  return false; // we reach this point when the two types don't match, and so we can say they're not equal

}


console.log(comp(obj, object1))


查看完整回答
反對 回復 2021-08-20
?
哆啦的時光機

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

您應(yīng)該使用for..of(或只是一個普通的 old for)而不是for..in僅用于對象。您現(xiàn)在正在閱讀數(shù)組索引,而不是實際的鍵名。Object.keys返回一個Arrayof 鍵名,而不是一個Object。

也不要早回來;現(xiàn)在,您在第一次鑰匙檢查后立即返回。


查看完整回答
反對 回復 2021-08-20
  • 3 回答
  • 0 關(guān)注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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