var name1 = { firstName: "emon", lastName: "zan" };
var name2 = { firstName: "lu", lastName: "zan" };

function isSameObject(ob1, ob2) {
    for (const key of Object.keys(ob1)) { //遍历对象的key
        if (ob1[key] !== ob2[key]) // 找到公共的key
         { return false; } 
        return true;
    } }
 

isSameObject(name1, name2); /// false

注释:

Object.keys(name1); //["firstName", "lastName"]

遍历对象并且以数组的方式返回索引值


Emon
8 声望1 粉丝