Forum Discussion

fazlook's avatar
fazlook
Frequent Contributor
6 years ago

Comparing JSON variable to an Array of objects in JScript

I would like to compare JSON values to an array of values but I d'ont know what's the best scenario to go with.

I got a JSON object with expected values (could have 1 value , 2 or more) I have a DB function that returns a fix number of values, say 10 values all the time and I would like to know if my JSON values matches the right one coming from DB. Ex:

My JSON var is :

 var expValues = {
            "id": "123",
            "age": 23
        };

My DB will push some values to an Array of objects.

Ex:

    if ((rs.BOF) && (rs.EOF))
    {
        //nothing found;
    }
    else
    {
        while (!rs.EOF)
        {
            aDetails.push(
            {
                "id": rs.fields("id").Value,
                "name": rs.fields("name").Value,
                "age": rs.fields("age").Value,
                "sex": rs.fields("sex").Value,
                "hobby": rs.fields("hobby").Value
            });
            rs.MoveNext();
        }
    }
     rs.close;
 //Close connection then return
 return aDetails;

This is doable easy using JavaScript but how could I do it with Jscript in testComplete ?

 

let aDetails = [{
  "id": "123",
   "name": "as",
   "age": 23,
   "sex": "m",
   "hobby": "abc"
}, {
  "id": "1234",
   "name": "as1",
   "age": 23,
   "sex": "m",
   "hobby": "abc"
}, {
  "id": "12",
   "name": "as2",
   "age": 23,
   "sex": "m",
   "hobby": "abc"
}]

var expValues = {
            "id": "123",
            "age": 23
        };
        
function isObjectMatched(obj) {
  return aDetails.some(d => Object.entries(obj).every(([k, v]) => d[k] == v))
}

console.log(isObjectMatched(expValues))

 

2 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    If you're using TestComplete 12... there's no need to pick between JScript and JavaScript.  If you have a project that is in JScript, you can convert easily.

    • fazlook's avatar
      fazlook
      Frequent Contributor
      We are not converting now. This is a task in summer 2019 :) so kinda I would like the JScript solution for now.