Created
December 21, 2015 21:50
-
-
Save ryedin/1be63d167a23ebe72b20 to your computer and use it in GitHub Desktop.
Ryan Gahl interview with Digital H2O
This file contains 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 characters
var _ = require('lodash'); | |
var hash1 = { | |
key1: 'abc', | |
key2: ['123', '222'] | |
}; | |
var hash2 = { | |
key2: 'def', | |
key4: '456' | |
}; | |
var hash3 = { | |
key5: 'ghi', | |
key6: '789' | |
}; | |
function hash_merge(_hash1, _hash2) { | |
var new_hash = {}; | |
for (var p in _hash1) { | |
new_hash[p] = _hash1[p]; | |
} | |
for (var p in _hash2) { | |
if (typeof new_hash[p] !== 'undefined') { | |
if (!_.isArray(new_hash[p])) new_hash[p] = [new_hash[p]]; | |
new_hash[p].push(_hash2[p]) | |
} else { | |
new_hash[p] = _hash2[p]; | |
} | |
} | |
return new_hash; | |
} | |
function merge_n() { | |
if (arguments.length === 1) { | |
return arguments[0]; | |
} else { | |
var new_hash = {}; | |
for (var i = 0; i <= arguments.length - 1; i++) { | |
new_hash = hash_merge(new_hash, arguments[i]); | |
} | |
return new_hash; | |
} | |
} | |
console.log(merge_n(hash1, hash2, hash3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment