-2

PS: I have already searched the forums and have seen the relevant posts for this wherein the same post exists but I am not able to resolve my issue with those solutions.

I have 2 json objects

var json1 = [{uid:"111", addrs:"abc", tab:"tab1"},{uid:"222", addrs:"def", tab:"tab2"}];

 var json2 = [{id:"tab1"},{id:"new"}];

I want to compare both these and check if the id element in json2 is present in json1 by comparing to its tab key. If not then set some boolean to false. ie by comparing id:"tab1" in json2 to tab:"tab1 in json1 .

I tried using below solutions as suggested by various posts:

     var o1 = json1;
     var o2 = json2;
     var set= false;
     for (var p in o1) {
        if (o1.hasOwnProperty(p)) {
             if (o1[p].tab!== o2[p].id) {
                  set= true;
                  }
             }
          }
     for (var p in o2) {
         if (o2.hasOwnProperty(p)) {
            if (o1[p].tab!== o2[p].id) {
                   set= true;
                }
            }
         }

Also tried with underscore as:

    _.each(json1, function(one) {
    _.each(json2, function(two) {
      if (one.tab!== two.id) {   
       set= true;
      }
    });
  });

Both of them fail for some test case or other.

Can anyone tell any other better method or outline the issues above.

user1563677
  • 713
  • 3
  • 15
  • 38
  • You want to set the boolean to `true` only if all the `id`s matched to `tab`s, or if any one of matches are found? – 31piy Aug 01 '18 at 03:01
  • Set Boolean to true if any element in json2 is not there in json1. Eg in above json id"new" in json2 is not present in json1 so set is true. – user1563677 Aug 01 '18 at 03:06
  • "I want to compare both these and check if the id element in json2". But there are multiple id properties in json2. "If not then set some boolean" Set a boolean if one element in json1 matches, or set different booleans for each element in json1? –  Aug 01 '18 at 04:25

2 Answers2

1

Don't call them JSON because they are JavaScript arrays. Read What is JSON.

To solve the problem, you may loop over second array and then in the iteration check if none of the objects in the first array matched the criteria. If so, set the result to true.

const obj1 = [{uid:"111", addrs:"abc", tab:"tab1"},{uid:"222",addrs:"def", tab:"tab2"}];
const obj2 = [{id:"tab1"},{id:"new"}];
let result = false;

for (let {id} of obj2) {
  if (!obj1.some(i => i.tab === id)) {
    result = true;
    break;
  }
}

console.log(result);
31piy
  • 23,323
  • 6
  • 47
  • 67
1

Unfortunately, searching the forums and reading the relevant posts is not going to replace THINKING. Step away from your computer, and write down, on a piece of paper, exactly what the problem is and how you plan to solve it. For example:

Calculate for each object in an array whether some object in another array has a tab property whose value is the same as the first object's id property.

There are many ways to do this. The first way involves using array functions like map (corresponding to the "calculate for each" in the question, and some (corresponding to the "some" in the question). To make it easier, and try to avoid confusing ourselves, we'll do it step by step.

function calculateMatch(obj2) {
  return obj2.map(doesSomeElementInObj1Match);
}

That's it. Your program is finished. You don't even need to test it, because it's obviously right.

But wait. How are you supposed to know about these array functions like map and some? By reading the documentation. No one help you with that. You have to do it yourself. You have to do it in advance as part of your learning process. You can't do it at the moment you need it, because you won't know what you don't know!

If it's easier for you to understand, and you're just getting started with functions, you may want to write this as

obj2.map(obj1Element => doesSomeElementInObj1Match(obj1Element))

or, if you're still not up to speed on arrow functions, then

obj2.map(function(obj1Element) { return doesSomeElementInObj1Match(obj1Element); })

The only thing left to do is to write doesSomeElementInObj2Match. For testing purposes, we can make one that always returns true:

function doesSomeElementInObj2Match() { return true; }

But eventually we will have to write it. Remember the part of our English description of the problem that's relevant here:

some object in another array has a tab property whose value is the same as the first object's id property.

When working with JS arrays, for "some" we have the some function. So, following the same top-down approach, we are going to write (assuming we know what the ID is):

In the same way as above, we can write this as

function doesSomeElementInObj2Match(id) {
  obj2.some(obj2Element => tabFieldMatches(obj2Element, id))
}

or

obj2.some(function(obj2Element) { return tabFieldMatches(obj2Element, id); })

Here, tabFieldMatches is nothing more than checking to make sure obj2Element.tab and id are identical.

We're almost done! but we still have to write hasMatchingTabField. That's quite easy, it turns out:

function hasMatchingTabField(e2, id) { return e2.tab === id; }

In the following, to save space, we will write e1 for obj1Element and e2 for obj2Element, and stick with the arrow functions. This completes our first solution. We have

const tabFieldMatches = (tab, id) { return tab === id; }
const hasMatchingTabField = (obj, id) => obj.some(e => tabFieldMatches(e.tab, id);
const findMatches = obj => obj.some(e => hasMatchingTabField(e1, obj.id));

And we call this using findMatches(obj1).

Old-fashioned array

But perhaps all these maps and somes are a little too much for you at this point. What ever happened to good old-fashioned for-loops? Yes, we can write things this way, and some people might prefer that alternative.

top: for (e1 of obj1) {
  for (e2 of (obj2) {
    if (e1.id === e2.tab) {
      console.log("found match");
      break top;
    }
  }
  console.log("didn't find match);
}

But some people are sure to complain about the non-standard use of break here. Or, we might want to end up with an array of boolean parallel to the input array. In that case, we have to be careful about remembering what matched, at what level.

const matched = [];

for (e1 of obj1) {
  let match = false;
  for (e2 of obj2) {
    if (e1.id === e2.tab) match = true;
  }
  matched.push(match);
}

We can clean this up and optimize it bit, but that's the basic idea. Notice that we have to reset match each time through the loop over the first object.