亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決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但它應該打印false
查看完整描述

3 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

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


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


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


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


請參閱下面的示例:


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條經驗 獲得超3個贊

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


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


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


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


請參閱下面的示例:


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條經驗 獲得超6個贊

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

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


查看完整回答
反對 回復 2021-08-20
  • 3 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號