Created
March 24, 2012 11:59
Revisions
-
ww24 revised this gist
Mar 24, 2012 . 3 changed files with 46 additions and 44 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,32 +1,15 @@ /* * 再帰的なオブジェクトのマージ 1 (クロスブラウザ) */ var merge = function merge(a, b) { for (key in b) { if (b.hasOwnProperty(key)) { a[key] = (key in a) ? ((typeof a[key] === "object" && typeof b[key] === "object") ? merge(a[key], b[key]) : b[key]) : b[key]; } } return a; }; // Usage This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,26 +1,13 @@ /* * 再帰的なオブジェクトのマージ 2 */ var merge = function merge(a, b) { Object.keys(b).forEach(function (key) { a[key] = (key in a) ? ((typeof a[key] === "object" && typeof b[key] === "object") ? merge(a[key], b[key]) : b[key]) : b[key]; }); return a; }; // Usage This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ /* * 再帰的なオブジェクトのマージ 3 (for Node.js 最適化) * 参考URL: http://d.hatena.ne.jp/hokaccha/20110728/1311866555 */ function merge() { return [].reduce.call(arguments, function merge(a, b) { Object.keys(b).forEach(function (key) { a[key] = (typeof a[key] === "object" && typeof b[key] === "object") ? a[key] = merge(a[key], b[key]) : a[key] = b[key]; }); return a; }); } // Usage var obj = { hoge: "hogehoge", piyo: [ "ピヨ", "piyopiyo" ] }; obj = merge(obj, {test: "hello", piyo: ["piyo"]}); console.log(obj); var a = {hoge: {key1: 'val1'}}; var b = {hoge: {key2: 'val2'}}; var c = {hoge: {key1: 'val3'}}; obj = merge(a, b, c); console.log(obj); -
ww24 revised this gist
Mar 24, 2012 . 2 changed files with 37 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ /* * 再帰的なオブジェクトのマージ 1 (クロスブラウザ) */ var merge = function (obj_a, obj_b) { var key = {}, This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ /* * 再帰的なオブジェクトのマージ 2 (for Node.js) */ var merge = function (obj_a, obj_b) { var merge = arguments.callee; Object.keys(obj_a).forEach(function (key_a) { Object.keys(obj_b).forEach(function (key_b) { if (key_a === key_b) { var val_a = obj_a[key_a], val_b = obj_b[key_b]; if (typeof val_a === 'object' && typeof val_b === "object") merge(val_a, val_b); else { obj_a[key_a] = val_b; } delete obj_b[key_b]; } }); }); Object.keys(obj_b).forEach(function (key) { obj_a[key] = obj_b[key]; }); return obj_a; }; // Usage var obj = { hoge: "hogehoge", piyo: [ "ピヨ", "piyopiyo" ] }; obj = merge(obj, {test: "hello", piyo: ["piyo"]}); console.log(obj); -
ww24 revised this gist
Mar 24, 2012 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,10 +11,10 @@ var merge = function (obj_a, obj_b) { if (hasOwn.call(obj_b, key.b) && key.a === key.b) { val.a = obj_a[key.a]; val.b = obj_b[key.b]; if (typeof val.a === 'object' && typeof val.b === "object") arguments.callee(val.a, val.b); else { obj_a[key.a] = val.b; } delete obj_b[key.b]; } -
ww24 revised this gist
Mar 24, 2012 . 1 changed file with 18 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,31 +2,28 @@ * 再帰的なオブジェクトのマージ */ var merge = function (obj_a, obj_b) { var key = {}, val = {}, hasOwn = Object.prototype.hasOwnProperty; for (key.a in obj_a) { if (hasOwn.call(obj_a, key.a)) { for (key.b in obj_b) { if (hasOwn.call(obj_b, key.b) && key.a === key.b) { val.a = obj_a[key.a]; val.b = obj_b[key.b]; if (typeof(val.a) === 'object' && typeof(val.b) === "object") { arguments.callee(val.a, val.b); } else { obj_a[key.a] = obj_b[key.b]; } delete obj_b[key.b]; } } } } for (key.b in obj_b) { if (hasOwn.call(obj_b, key.b)) { obj_a[key.b] = obj_b[key.b]; } } return obj_a; @@ -40,4 +37,6 @@ var obj = { "piyopiyo" ] }; obj = merge(obj, {test: "hello", piyo: ["piyo"]}); console.log(obj); -
ww24 created this gist
Mar 24, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ /* * 再帰的なオブジェクトのマージ */ var merge = function (obj_a, obj_b) { var obj = {}, key_a, key_b, val_a, val_b, hasOwn = Object.prototype.hasOwnProperty; for (key_a in obj_a) { if (hasOwn.call(obj_a, key_a)) { for (key_b in obj_b) { if (hasOwn.call(obj_b, key_b) && key_a === key_b) { val_a = obj_a[key_a]; val_b = obj_b[key_b]; if (typeof(val_a) === 'object' && typeof(val_b) === "object") { obj_a[key_a] = arguments.callee(val_a, val_b); } else { obj_a[key_a] = obj_b[key_b]; } delete obj_b[key_b]; } } } } for (key_b in obj_b) { if (hasOwn.call(obj_b, key_b)) { obj_a[key_b] = obj_b[key_b]; } } return obj_a; }; // Usage var obj = { hoge: "hogehoge", piyo: [ "ピヨ", "piyopiyo" ] }; merge(obj, {test: "hello", piyo: ["piyo"]});