0

My goal is to compare two different JSON services and show the difference in a seperate div. Service A shows lists a user is enrolled in. Service B shows all lists available. I want to be able to show the user's lists in one div and the remaining lists from the major list in another.

Both sets of info come from internal JSON services. My first approach was to take the list ID's from each (they are the same)and store them in array's and compare the arrays. Service A(ob1):

1,2,3,4

Service B(ob2):

-1,1,2,3,4

Here is the comparison, which started from another SO post

$.grep(ob1, function (el) {
        if ($.inArray(el, ob2) == -1) diff.push([el, IDl]);
    });

result: If a user has 1 list ID then only a single result with the ID from the last item in the JSON call, instead of 4 with the name associated. The associated name is the "IDl" being pushed into the last array. Any help would be great.

Community
  • 1
  • 1
atlMapper
  • 764
  • 3
  • 9
  • 24
  • I don't understand your problem. You want to convert your ID into, what? A name? I don't see any names in your question. – Blazemonger Dec 18 '12 at 14:52
  • I want to compare the ID's and list the ID's that are the same in one div and the ID's that are different in another. The ID's are associated with a name and I'm passing the associated name into the final array (diff) – atlMapper Dec 18 '12 at 14:54
  • What's the point of using `grep` but returning `undefined`? Did you want a simple for-loop? – Bergi Dec 18 '12 at 14:59
  • @Bergi I'm unsure, are you suggesting I run a for loop to compare ob1 !== ob2 – atlMapper Dec 18 '12 at 15:21

2 Answers2

0

You want the disjoint of the two arrays to serve as the second array.

var ob2 = $.grep(ob2, function(elem) { return $.inArray(elem, ob1) == -1; });

Then you can display the contents of ob1 and ob2.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • So this is giving me the same response as the loop I created. Is there a better way to achieve the goal of comparing two JSON calls and displaying the items that are the same and different? I feel like I'm over complicating this concept – atlMapper Dec 18 '12 at 15:18
  • @atlMapper it works and it's one line .. how is that over complicated? – Explosion Pills Dec 18 '12 at 15:21
  • thanks, and no not your solution, but my approach: is dumping the data into an array and comparing the arrays the best method? – atlMapper Dec 18 '12 at 15:24
0

Loop over your bigger array instead of your smaller one:

$.grep(ob2, function (el) {
    if ($.inArray(el, ob1) == -1) diff.push([el, IDl]);
});
freejosh
  • 11,263
  • 4
  • 33
  • 47
  • So something seems weird. when I'm running this all this is doing is relisting all the values from ob2 into the new array. I didn't notice before diff = ob2 – atlMapper Dec 18 '12 at 15:14
  • @atlMapper yea, it's copying `ob2`'s values into `diff` if they're not present in `ob1`. Isn't that what you asked? I wasn't sure what you were intending with the `IDl` part so I left it as you had it. – freejosh Dec 18 '12 at 16:30
  • thanks, that's executing properly I'm going to modify this a bit. this was a good start though...much appreciated – atlMapper Dec 18 '12 at 16:42