最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Elminating duplicates in a JSON object - Stack Overflow

programmeradmin1浏览0评论
[
  {
    "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...

[
  {
    "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...

Share Improve this question edited Jun 29, 2011 at 10:09 Robert Koritnik 105k56 gold badges284 silver badges413 bronze badges asked Jun 29, 2011 at 9:45 John CooperJohn Cooper 7,64133 gold badges83 silver badges102 bronze badges 1
  • 3 good question, i'd like to know if there's a way other than comparing all values. +1 – Ruslan Commented Jun 29, 2011 at 9:47
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论