2012-01-19 7 views
0

私は他のオブジェクトからのデータを更新したいjavascriptオブジェクトを持っています。私は新しいオブジェクトから新しいものを追加し、新しいオブジェクトを使って古いものを更新し、新しいオブジェクトにない古いオブジェクトには何も残したくない。 (一番下の例)2つのJavaScriptオブジェクトからプロパティをマージする最適な方法は何ですか?

私はさまざまな方法を試してみましたが、彼らは完全にオブジェクトを上書きするのどちらかか、手動でオブジェクト内のオブジェクト内のオブジェクトのためのループを記述することなくカスケードをない、などが

私もについて考えていますそれは、それ自体の内部、別のオブジェクトを持っているかどうかを確認し、それがない場合は、オブジェクトを更新するすべてながら、自分自身を呼び出して、プロパティを反復します再帰関数。 {id:1, name:"asdf", info:{first:3, second:{deeper:5, new_deeper:6}, third:7}, new_info:7};

がでありがとう:

var obj1 = {id:1, name:"asdf", info:{first:3, second:{deeper:3} } };

var obj2 = {id:1, info:{first: 3, second:{deeper:5, new_deeper:6}, third:7}, new_info:7};

私はそれはそうと同等であることOBJ1したいと思います(何かクリーナーを期待して、それを書いていません)前進!

+0

アルゴリズムは不十分ビルドされています。私は – Raynos

+0

同様の質問ここにあなたのデータ構造を平坦化をお勧めします:http://stackoverflow.com/questions/728360/copying-an-object-in-javascript。そこにはたくさんのコードがあります。これは、 "copy object javascript"または "clone object javascript"を少し検索すると実行されます。 – jfriend00

答えて

2

私は図書館を示唆して知っているが、常に偉大な答えはありませんが、jQueryのは、これを行うためのキラーである$.extend方法があります。

// Add TRUE as the last parameter to copy nested objects 
var newObj = $.extend(objectA, objectB, true); 

ライブラリをチェックすると、その機能だけを取得して自分のものに使うことができます。深い再帰的な拡張を必要と

http://code.jquery.com/jquery-1.7.1.js

jQuery.extend = function() { 
    var options, name, src, copy, copyIsArray, clone, 
     target = arguments[0] || {}, 
     i = 1, 
     length = arguments.length, 
     deep = false; 

    // Handle a deep copy situation 
    if (typeof target === "boolean") { 
     deep = target; 
     target = arguments[1] || {}; 
     // skip the boolean and the target 
     i = 2; 
    } 

    // Handle case when target is a string or something (possible in deep copy) 
    if (typeof target !== "object" && !jQuery.isFunction(target)) { 
     target = {}; 
    } 

    // extend jQuery itself if only one argument is passed 
    if (length === i) { 
     target = this; 
     --i; 
    } 

    for (; i < length; i++) { 
     // Only deal with non-null/undefined values 
     if ((options = arguments[ i ]) != null) { 
      // Extend the base object 
      for (name in options) { 
       src = target[ name ]; 
       copy = options[ name ]; 

       // Prevent never-ending loop 
       if (target === copy) { 
        continue; 
       } 

       // Recurse if we're merging plain objects or arrays 
       if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) { 
        if (copyIsArray) { 
         copyIsArray = false; 
         clone = src && jQuery.isArray(src) ? src : []; 

        } else { 
         clone = src && jQuery.isPlainObject(src) ? src : {}; 
        } 

        // Never move original objects, clone them 
        target[ name ] = jQuery.extend(deep, clone, copy); 

       // Don't bring in undefined values 
       } else if (copy !== undefined) { 
        target[ name ] = copy; 
       } 
      } 
     } 
    } 

    // Return the modified object 
    return target; 
}; 
+0

これはまさに私が探していたものです! $ .extend(真、OBJ1、OBJ2)。チャンピオンのように動作し、私は厄介な再帰的混乱を書く必要はありません。 迅速な対応をありがとうございます! – user915134

関連する問題