0

I have 2 JSON arrays, both of which can contain nested arrays:

var serverArr = [
              { "id": 1, "text": "Item 1" },
              { "id": 2, "text": "Item 2" },
              { "id": 3, "text": "Item 3", "children": [{ "id": 20, "text": "Item 20" },
                                                        { "id": 21, "text": "Item 21" }] },
              { "id": 4, "text": "Item 4" }
           ];

var userArr = [
                 { "id": 1, "text": "Item 1" },
                 { "id": 3, "text": "Item 3", "children": [{ "id": 20, "text": "Item 20" },
                                                           { "id": 25, "text": "Item 25" }] },
                 { "id": 5, "text": "Item 5" }
              ];

What I need to do is combine them into 1 array, only taking the matching values. So the result should look like:

 [{ "id": 1, "text": "Item 1" },
  { "id": 3, "text": "Item 3", "children": [{ "id": 20, "text": "Item 20" }]}];

I'm using the results with the jsTree plugin, so the format has to be like this unfortunately.

How can I get the end result from those 2 arrays?

user1147941
  • 121
  • 2
  • 16
  • This is a tricky question as technically the id 3 object is not the same, so any logic would get very specific and messy. You essentially want to say "I know this object is not the same, but somehow check every property in the object and see if it has anything shared with the other object and then create a new object from that". I would be surprised if you get an answer for that but here is a starting point (array-object intersection) - http://jsfiddle.net/9AJ6n/1/ or http://jsfiddle.net/9AJ6n/2/ – Dominic Jul 28 '14 at 22:15
  • Taken from here: http://stackoverflow.com/questions/8672383/how-to-use-underscores-intersection-on-objects maybe there is some more info there for you – Dominic Jul 28 '14 at 22:16
  • Here's a fiddle that will do what you're after, but as Dominic says - id: 3 is not actually identical, so comparing the objects wont help. You might just need to compare id, text and whether or not there are any children (regardless of what they are). If you're after exact matches, [this fiddle](http://jsfiddle.net/scrowler/u6kLt/) might help you. – scrowler Jul 28 '14 at 22:18

1 Answers1

0

I have defined a function that does 3 things to solve this problem

  • First thing it gets all the elements that are in both arrays and it collects all childs for every item in both arrays

  • Second thing is filtering all the childs elements to keep only unique ones

  • Third thing is changing the children of the parent elements to keep only unique ones

        function combine(array1,array2)
        {
    
            result = serverArr.concat(userArr);
            var childs = [];
    
            // First step
    
            result = result.filter(function(elem, index, self) 
            {
                if(elem.children != undefined)
                    childs[elem.id] = ((childs[elem.id] == undefined) ? [] : childs[elem.id]).concat(elem.children);
                for (key in self) 
                    if(key < index && elem.id == self[key].id )
                        return true;
                return false;
            });
    
            // Second step
    
            for(i in childs)
                childs[i] = childs[i].filter(function(elem, index, self)
                {
                    for (key in self) 
                        if(key < index && JSON.stringify(elem) == JSON.stringify(self[key]) )
                            return true; 
                    return false;
                });
    
            //Third step
    
            for(key in result)
                if(childs[result[key].id] != undefined)
                    result[key].children = childs[result[key].id];
    
            return result;
        }
    
Khalid
  • 4,730
  • 5
  • 27
  • 50