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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…