Javascript Deep Comparison

后端 未结 3 443
感动是毒
感动是毒 2021-01-21 05:35

Questions about deep comparison of objects have been asked, and I have the solution. But there is a line in the solution that I don\'t completely understand.

This is the

3条回答
  •  温柔的废话
    2021-01-21 06:01

    The in operator returns true if the object on the right of the expression contains a key with the value of the string on the left of the expression.

    Eg: prop in a is true if a contains a key with the same name as the string value of prop. Eg:

    var prop = "age";
    
    var obj1 = {
        name: "Dave",
        age: 21,
        record: { ... }
    };
    var obj2 = {
        name: "John",
        record: { ... }
    };
    
    console.log(prop in obj1); // true
    console.log(prop in obj2); // false
    

    If prop was set to "record" then deepEqual(a[prop], b[prop]) recursively compares the values in a.record and b.record.

提交回复
热议问题