两个对象之间的差异并添加缺失值

时间:2016-08-23 14:03:59

标签: javascript

如何检查两个对象之间的差异并添加缺失值?

示例:https://jsfiddle.net/aca0hdv9/

 var defaultOptions = {
         monit : {
            map:{
                googleMap: false
            },
            vehiclesList: {
                rightSide: false,
                number: true,
                driver: true,
                note: true,
            },
            vehicleCloud: {
                speed: true,
                date: true,
                alarms: true,
                fuelsCloud: true
            },
            driverCloud: true
     };

var userCacheOptions = {
        monit : {
                // Missing map options
            vehiclesList: {
                rightSide: false,
                number: false,
                driver: false
                // Missing note option
            },
            vehicleCloud: {
                speed: true,
                date: true,
                alarms: true,
                fuelsCloud: true
            },
            // Missing driverCloud option
         }
    };

我不想分配默认值,我只想添加缺少值。知道如何以最简单的方式做到这一点吗?

4 个答案:

答案 0 :(得分:1)

您可以使用Object.keys获取所有可枚举的密钥。从那里,你只需要迭代它们。



var defaultOptions = {
  monit: {
    map: {
      googleMap: false
    },
    vehiclesList: {
      rightSide: false,
      number: true,
      driver: true,
      note: true,
    },
    vehicleCloud: {
      speed: true,
      date: true,
      alarms: true,
      fuelsCloud: true
    },
    driverCloud: true
  }
};

var userCacheOptions = {
  monit: {
    // Missing map options
    vehiclesList: {
      rightSide: false,
      number: false,
      driver: false
        // Missing note option
    },
    vehicleCloud: {
      speed: true,
      date: true,
      alarms: true,
      fuelsCloud: true
    }
    // Missing driverCloud option
  }
};

function fillBlanks(receiver, giver) {
  var receiverkeys = Object.keys(receiver);
  var giverkeys = Object.keys(giver);
  for(var i = 0; i < giverkeys.length; i++) {
    if(typeof receiver[giverkeys[i]] != typeof giver[giverkeys[i]]) {
      receiver[giverkeys[i]] = giver[giverkeys[i]];
    }
    if(receiver[giverkeys[i]] != giver[giverkeys[i]]) {
      receiver[giverkeys[i]] = giver[giverkeys[i]];
    }
  }
}
fillBlanks(userCacheOptions.monit, defaultOptions.monit);
console.log(userCacheOptions.monit);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以迭代键并检查对象,并使用要检查的新对象来递归调用函数。如果缺少值,则将其设置为默认值。

&#13;
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
userInteractionEnabled
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我会按照以下方式完成这项工作;它递归地迭代并修改了userCacheOptions对象。

var defaultOptions = {
         monit : {
            map:{
                googleMap: false
            },
            vehiclesList: {
                rightSide: false,
                number: true,
                driver: true,
                note: true,
            },
            vehicleCloud: {
                speed: true,
                date: true,
                alarms: true,
                fuelsCloud: true
            },
            driverCloud: true
           }
     };

var userCacheOptions = {
        monit : {
                // Missing map options
            vehiclesList: {
                rightSide: false,
                number: false,
                driver: false
                // Missing note option
            },
            vehicleCloud: {
                speed: true,
                date: true,
                alarms: true,
                fuelsCloud: true
            },
            // Missing driverCloud option
         }
    };

function restoreMissing(def,obj){
  Object.keys(def).map(k => (typeof def[k] === "object" &&
                             typeof obj[k] === "object") ? restoreMissing(def[k],obj[k])
                                                         : !obj[k] && (obj[k] = def[k]));
  return obj;
}

console.log(restoreMissing(defaultOptions,userCacheOptions))

答案 3 :(得分:0)

这是您要遵循的模式,您只需为每个检查添加适当的操作。

if(!(defaultOptions.equals(userCacheOptions))){

        //check for userCacheOptions if the value is set
        //if its not set then set it as you want

        if(!(userCacheOptions.getGoogleMap().isSet())){
            userCacheOptions.setGoogleMap("whatever you want to set");
        }

        //same each variable etc
    }