fazlook1
fazlook1

Reputation: 139

How best to Compare JSON object values to a fixed array 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;

basically I want to make sure values coming from JSON match the right ones coming from DB. (id for example).

Upvotes: 0

Views: 79

Answers (2)

Dominique Fortin
Dominique Fortin

Reputation: 2238

This is a general purpose way of indexing list of objects for fast retrieval with any configuration of properties.

// javascript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
  var index = {};

  index.objToKey = function (o) {
    var key = [];
    listOfPropertyToIndex.forEach((p) => {
      key.push(""+o[p]);
    });
    return key.join("_");
  };

  arrayOfObject.forEach((o) => {
    index[objToKey(o)] = o;
  });

  index.match = function (object) {
    var key = index.objToKey(object);
    if (index.hasOwnProperty(key)) {
      return index[key];
    };
    return null;
  });

  return index;
}

// jscript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
  var index = {};

  index.objToKey = function (o) {
    var key = [];
    for (var p in o) {
      if (o.hasOwnProperty(p)) {
        key.push(""+o[p]);
      }
    }
    return key.join("_");
  };

  for (var i = 0; i < arrayOfObject.length; ++i) {
    index[objToKey(arrayOfObject[i])] = o;
  } 

  index.match = function (object) {
    var key = index.objToKey(object);
    if (index.hasOwnProperty(key)) {
      return index[key];
    };
    return null;
  });

  return index;
}

Here is how to use it

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

var index = makeIndex(aDetails, ["id","age"]);

var obj = index.match(expValues);
if (obj) {
   ... obj ...
}

var index_name = makeIndex(aDetails, ["name"]);

var person = {"name":"as2"};

var obj2 = index_name.match(person);
if (obj2) {
   ... obj2 ...
}

Upvotes: 0

Nitish Narang
Nitish Narang

Reputation: 4184

I have assumed aDetails to have something like below data.

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))

Upvotes: 4

Related Questions