【问题标题】:How to check if an objects property exists in another one too?如何检查对象属性是否也存在于另一个对象中?
【发布时间】:2019-06-22 00:43:44
【问题描述】:

我有两个不同的对象,如果第二个对象的至少一个属性与第一个匹配,我想返回 true。如果不是,则返回 false。我尝试过 .hasOwnProperty 和 .keys 方法,但无法处理。下面是一个示例代码。谢谢。

let propchecker = (a,b) =>{
    if(/* at least one property matches exact */){
        return true;
     }

    else {
        return false;
    }
}

let origin = {name: "John", surname: "Doe"};

let first = {name: "John", surname: "Roe" };
let second = {name: "Jane", surname: "Doe"};
let third = {name: "Richard", surname: "Roe"};

console.log(propchecker(origin,first)); //Should return True.
console.log(propchecker(origin,second)); //Should return True.
console.log(propchecker(origin,third)); //Should return False.

【问题讨论】:

  • 应该尽可能通用还是检查姓名和姓氏就足够了?
  • 泛型应该更好

标签: javascript


【解决方案1】:

可能有更短的解决方案,但这很好用

let propchecker = (a,b) =>{
for (let key in a) {
  for (let key2 in b ){
    if(a[key] == b[key2]){
     return true
   }
 }
}
return false
}

let origin = {name: "John", surname: "Doe"};

let first = {name: "John", surname: "Roe" };
let second = {name: "Jane", surname: "Doe"};
let third = {name: "Richard", surname: "Roe"};

console.log(propchecker(origin,first)); //Should return True.
console.log(propchecker(origin,second)); //Should return True.
console.log(propchecker(origin,third)); //

【讨论】:

    【解决方案2】:

    您可以使用Object.keys() 获取一个对象的键,然后在其上使用some()

    let propchecker = (a,b) => Object.keys(a).some(x => a[x] === b[x])
    
    let origin = {name: "John", surname: "Doe"};
    
    let first = {name: "John", surname: "Roe" };
    let second = {name: "Jane", surname: "Doe"};
    let third = {name: "Richard", surname: "Roe"};
    
    console.log(propchecker(origin,first)); //Should return True.
    console.log(propchecker(origin,second)); //Should return True.
    console.log(propchecker(origin,third)); //Should return False.

    【讨论】:

    • 这是较短的解决方案,不错
    • @SirDad 如果认为最适合您,请考虑接受他的回答。
    • 是的,当然,兄弟,它应该是最好的答案,谢谢
    【解决方案3】:

    试试这个

    let propchecker = (a,b) =>{
       result =  false;
       for(let ele in first){
           result  = result  || (a[ele] === b[ele])
        }
       return result
     }
    
    let origin = {name: "John", surname: "Doe"};
    
    let first = {name: "John", surname: "Roe" };
    let second = {name: "Jane", surname: "Doe"};
    let third = {name: "Richard", surname: "Roe"};
    
    console.log(propchecker(origin,first)); //Should return True.
    console.log(propchecker(origin,second)); //Should return True.
    console.log(propchecker(origin,third)); //Should return False.

    【讨论】:

      【解决方案4】:

      看起来它已经被回答了,但我也会把我的帽子扔进擂台上。所有这些答案都在做几乎相同的事情。

      let propchecker = (a,b) =>{
          var check = false;
          Object.keys(a).forEach(function(key) {
              if(a[key] == b[key]) {
                  check = true;
              }
          })
      
          return check;
      }
      
      let origin = {name: "John", surname: "Doe"};
      
      let first = {name: "John", surname: "Roe" };
      let second = {name: "Jane", surname: "Doe"};
      let third = {name: "Richard", surname: "Roe"};
      
      console.log(propchecker(origin,first)); //Should return True.
      console.log(propchecker(origin,second)); //Should return True.
      console.log(propchecker(origin,third)); //Should return False.
      

      【讨论】:

        猜你喜欢
        • 2020-11-30
        • 1970-01-01
        • 2016-06-05
        • 1970-01-01
        • 1970-01-01
        • 2021-06-08
        • 1970-01-01
        • 2019-11-26
        • 1970-01-01
        相关资源
        最近更新 更多