【问题标题】:Is there a built in method to merge the properties of two objects into a single object?是否有内置方法可以将两个对象的属性合并为一个对象?
【发布时间】:2009-06-11 03:02:40
【问题描述】:

我正在寻找一种将两个关联数组或对象合并为一个的内置方法。如果有不同,请在 Adob​​e Air 中使用 webkit。但基本上我有两个对象或关联数组,如果你愿意的话:

var obj1 = { prop1: "something", prop2 "anotherthing" };
var obj2 = { prop3: "somethingelse" };

我想合并它们并创建一个包含上述两个对象的所有组合键和值的对象:

var obj3 = obj1.merge( obj2 ); //something similar to array's concat maybe?

alert(obj3.prop1); //alerts "something"
alert(obj3.prop2); //allerts "anotherthing"
alert(obj3.prop3); //alerts "somethingelse"

有任何内置函数可以执行此操作还是我必须手动执行此操作?

【问题讨论】:

    标签: javascript merge webkit air


    【解决方案1】:

    就像 tryptych 所说,除了他的示例代码(在他编辑之前是危险和错误的)。像下面这样的东西会更好。

    mylib = mylib || {};
    //take objects a and b, and return a new merged object o;
    mylib.merge = function(a, b) {
    
      var i, o = {};
      for(i in a) {
          if(a.hasOwnProperty(i)){
              o[i]=a[i];
          }
      }
      for(i in b) {
          if(b.hasOwnProperty(i)){
              o[i]=b[i];
          }
      }
    
      return o;
    
    }
    //take objects a and b, and modify object a to have b's properties
    mylib.augment = function(a, b) {
      var i;
      for(i in b) {
          if(b.hasOwnProperty(i)){
              a[i]=b[i];
          }
      }
      return a;
    }
    

    编辑回复:凶猛。深拷贝是一个不同的、正交的函数,但只是为了你,这是我个人的深拷贝函数

       function clone(o) {
        var t,i;
        if (o === undefined) {
            return undefined;
        }
        if (o === null) {
            return null;
        }
        if (o instanceof Function) {
            return o;
        }
        if (! (o instanceof Object)) {
            return o;
        } else {
            t = {};
            for (i in o) {
                /* jslint complains about this, it's correct in this case. I think. */
                t[i] = clone(o[i]);
            }
            return t;
        }
       }
    

    【讨论】:

    • 我喜欢这个,但是你能修改它来做一个深拷贝(复制里面的对象)吗?
    • done, (sorta), mylib.merge(clone(a),clone(b)) 基本上是你想要的效果。
    【解决方案2】:

    Triptych 的替代实现(与 Prototype 的 extend 方法基本相同)是:

    /** returns object with properties of both object1 and object2. 
      *  if a collision occurs, properties of object1 take priority
      */
    function merge(object1,object2) {
        var retObj = {};
        for (prop in object2){
            retObj[prop] = object2[prop];
        }
        for (prop in object1){
            retObj[prop] = object1[prop];
        }
        return retObj;
    }
    

    这并没有修改 Object 的原型,因此没有它不是微不足道的缺点。

    【讨论】:

      【解决方案3】:

      没有内置方法。有几个库提供了一种方法来执行您所描述的操作。

      自己写一个很简单:

      var merge = function(dest, source){
          // This will resolve conflicts by using the source object's properties
          for (prop in source){
              dest[prop] = source[prop];
          }
      }
      
      // Use like so
      merge(obj1, obj2);
      

      编辑:不再修改 Object.prototype,这很危险,通常不受欢迎。

      【讨论】:

      • 小心这个实现。如果您在代码中的任何位置都有 for( x in obj) 循环,则 for 循环将遍历 merge 函数,除非您在所有循环中显式测试它。这可能不是你想要的。
      • 这太糟糕了,使用带有大对象的纯 javascript 会很慢。可惜它不存在。有这样的东西是有道理的,不是吗/
      • 另外,它会修改原来的对象,也可能不是你想要的。
      • “这太糟糕了,使用带有大对象的纯 javascript 会很慢。可惜它不存在。拥有这样的东西很有意义,不是吗?” Ecmascript 5 有 Object.create,它可以帮助你做这样的事情。 Ecmascript 5 是新的 javascript 标准,我预计它将很快被浏览器采用。
      猜你喜欢
      • 2010-10-08
      • 2017-01-17
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多