Join dictionaries of arrays in JavaScript -
first of all, come obj-c , python, feel free edit faults in javascript teminology.
i looking efficient way of joining multiple dictionaries in javascript, each key can exist in more 1 dictionary , values arrays.
for example, have:
{foo: [1,2], bar: [3,4]} {foo: [5,6], baz: [7,8]}
and want join values of same key, meaning should return:
{foo: [1,2,5,6], bar: [3,4], baz: [7,8]}
i started doing following pseudo code, feel there should more efficient way of doing it.
// pseudo code return_value = {} (subset in full_array) (kv in subset) data = return_value[kv] || [] data.push(subset[kv]) return_value[kv] = data
with lo-dash:
var data = [ {foo: [1,2], bar: [3,4]}, {foo: [5,6]}, ]; _.reduce(data, function (result, obj) { _.each(obj, function (array, key) { result[key] = (result[key] || []).concat(array) }) return result }, {})
see fiddle.
Comments
Post a Comment