2

First of all, I come from Obj-C and Python, so feel free to edit any faults in my JavaScript teminology.

I am looking for an efficient way of joining multiple dictionaries in JavaScript, where each key can exist in more than one dictionary and its values are arrays.

For example, I have:

{foo: [1,2], bar: [3,4]}
{foo: [5,6], baz: [7,8]}

And I want to join all the values of the same key, meaning I should return:

{foo: [1,2,5,6], bar: [3,4], baz: [7,8]}

I started doing something like the following pseudo code, but I feel like there should be a more efficient way of doing it.

// Pseudo code
return_value = {}
for (subset in full_array)
    for (kv in subset)
        data = return_value[kv] || []
        data.push(subset[kv])
        return_value[kv] = data
Daniel Larsson
  • 6,278
  • 5
  • 44
  • 82
  • 2
    How should it handle duplicate values? – Johan Aug 18 '14 at 22:57
  • That's about the way I'd do it. – Lucas Trzesniewski Aug 18 '14 at 22:57
  • I think there is a mistake. If you have Arrays of Dictionaries, is the result should be [{foo: [1,2,5,6], bar: [3,4]}, {foo:[5,6]}] ? You join both Arrays nor Dictionaries. – Alejandro F. Carrera Aug 18 '14 at 22:57
  • @Johan it should simply append one value to another, making it a single array. – Daniel Larsson Aug 18 '14 at 22:58
  • on a terminology note, in JavaScript those are called objects, not dictionaries (which is useful to know if you're asking people familiar with JavaScript about this in the future). Also, that code doesn't look like javascript at all. The for loops are missing `{...}` enclosures and will cause errors. Is this actually CoffeeScript? – Mike 'Pomax' Kamermans Aug 18 '14 at 23:12
  • @Mike'Pomax'Kamermans thanks for the note. I know that it is not valid JS, that was why I mentioned that it was pseudo code. My question is more regarding the algorithm than its syntax. – Daniel Larsson Aug 18 '14 at 23:17
  • this leaves the question of "what are you doing that requires you to do this", because you might not be taking advantage of things JS can do, instead applying python(esque?) ideas to a language that is very different in certain respects. – Mike 'Pomax' Kamermans Aug 18 '14 at 23:20
  • @Mike'Pomax'Kamermans I understand. The reason is this: I have a bunch of nodes that all does computation, and they return a dictionary (object) containing a bunch of arrays. This gives me n number of dictionaries that I need to sort, but in order to to that I would first have to join them into one dictionary. – Daniel Larsson Aug 18 '14 at 23:29

2 Answers2

1

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 this fiddle.

Hugo Wood
  • 2,140
  • 15
  • 19
0

This code works:

var joinManyObjects = function joinManyObjects (arrayA, arrayB) 
{
  var i, j;
  for(i = 0; i < arrayB.length; i++)
  {
    for(j = 0; j < arrayA.length; j++)
    {
      var k = Object.keys(arrayB[i]);
      console.log("Common keys: "+k);
      if(k in arrayA[j])
      {
        arrayA[j][k] = arrayA[j][k].concat(arrayB[i][k]);
      }
    }
  }
}

var a = [{foo: [1,2], bar: [3,4]}];
var b = [{foo: [5,6]}];

joinManyObjects(a,b) -> a = [{"foo":[1,2,5,6],"bar":[3,4]}]

You can add remove duplicates or type detection (typeof or Array.isArray).

Community
  • 1
  • 1