13
[
  {
    "lastName": "Seymour",
    "gender": "Female",
    "patientID": 18134,
    "firstName": "Stephanie",
    "age": "111Y"
  },
  {
    "lastName": "Seymour",
    "gender": "Female",
    "patientID": 18134,
    "firstName": "Stephanie",
    "age": "111Y"
  }
]

How can i check my json before adding whether it contains this value or not...

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
John Cooper
  • 7,343
  • 31
  • 80
  • 100

2 Answers2

14

Obvious way

The most obvious way would be to use a for loop and iterate over all items in array and compare every time when you'd like to know whether you already have a particular item in it. Trivial but tedious especially when you have many compares to make.

function contains(items, obj) {
    var itemKey = null;
    var objKey = obj.firstName + ";" + obj.lastName;
    var result = false;
    for(var i = 0; !result && i < arr.length; i++)
    {
        itemKey = items[i].firstName + ";" + items[i].lastName;
        result = itemKey === objKey;
    }
    return result;
};

Whenever you'd be searching for existing object:

contains(arr, newObj);

Smart(er) way

This code uses the ability of Javascript whose prototypes can be used as arrays in a sort of associative memory storage. Suppose you have your JSON items in arr variable. Define your compare key (say it's first name and last name):

var arr = /* your JSON object */;
var hash = (function() {
    var keys = {};
    return {
        contains: function(key) {
            return keys[key] === true;
        },
        add: function(key) {
            if (keys[key] !== true)
            {
                keys[key] = true;
            }
        }
    };
})();

var key = null;
for (var i = 0; i < arr.length; i++)
{
    key = arr[i].firstName + ";" + arr[i].lastName;
    if (!hash.contains(key))
    {
        hash.add(key);
    }
}

Finding out whether you already have an item in your array is then pretty simple:

hash.contains(key);

And that's it. Much faster than iterating over the whole array. Especially when it has many items.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • Koritnik: Thanx for the answer, could you also show me the other way of iterating over the array... coz mine just has 5 - 10 items alone – John Cooper Jun 29 '11 at 10:00
  • Kortnik: Can you tell what's wrong in this jsfiddle?? http://jsfiddle.net/spechackers/eMbmw/ – John Cooper Jul 04 '11 at 12:16
  • 2
    @John: Nothing. I debugged it and it works as expected. When it gets to the first object in array it stores its key and when it comes to the second one, it doesn't because it's already there. You probably expect it to eliminate objects from your original array... Ok. Let me change it for you... [Here you go](http://jsfiddle.net/eMbmw/1/) – Robert Koritnik Jul 05 '11 at 09:50
  • for the same question if firstnames are same i want to add ages from the two arrays. how can we do that – shiva Aug 08 '14 at 07:53
0

what is "contains this value"? do you mean, the two items have all the fields equal?

in this case you may serialize to string (json) each item and compare the new one (serialized also) to the set of existing.

zmila
  • 1,651
  • 1
  • 11
  • 13