簡體   English   中英

如何合並兩個JSON對象數組 - 刪除重復項並保留Javascript / jQuery中的順序?

[英]How to merge two arrays of JSON objects - removing duplicates and preserving order in Javascript/jQuery?

jsfiddle鏈接: http//jsfiddle.net/vN6fn/1/

假設我有這兩個對象:

var obj1 = { data: [ 
                      {id:1, comment:"comment1"},
                      {id:2, comment:"comment2"},
                      {id:3, comment:"comment3"}
                   ] }

var obj2 = { data: [ 
                      {id:2, comment:"comment2"},
                      {id:3, comment:"comment3"},
                      {id:4, comment:"comment4"}
                   ] }

最終對象應如下所示:

var final = { data: [ 
                      {id:1, comment:"comment1"},
                      {id:2, comment:"comment2"},
                      {id:3, comment:"comment3"},
                      {id:4, comment:"comment4"}
                   ] }

這里有一些要考慮的事情:

  • obj1和obj2可能有也可能沒有重復

$.extend()替換對象, $.merge()不會刪除重復項(我知道我可以為循環執行,但我正在尋找更好的方法來執行此操作)。

您可以使用$.merge然后瀏覽並刪除重復項,然后對其進行排序。

$.merge(obj1.data, obj2.data);

var existingIDs = [];
obj1.data = $.grep(obj1.data, function(v) {
    if ($.inArray(v.id, existingIDs) !== -1) {
        return false;
    }
    else {
        existingIDs.push(v.id);
        return true;
    }
});

obj1.data.sort(function(a, b) {
    var akey = a.id, bkey = b.id;
    if(akey > bkey) return 1;
    if(akey < bkey) return -1;
    return 0;
});

這是一個直接的jQuery解決方案:

function mergeDeep(o1, o2) {
    var tempNewObj = o1;

    //if o1 is an object - {}
    if (o1.length === undefined && typeof o1 !== "number") {
        $.each(o2, function(key, value) {
            if (o1[key] === undefined) {
                tempNewObj[key] = value;
            } else {
                tempNewObj[key] = mergeDeep(o1[key], o2[key]);
            }
        });
    }

    //else if o1 is an array - []
    else if (o1.length > 0 && typeof o1 !== "string") {
        $.each(o2, function(index) {
            if (JSON.stringify(o1).indexOf(JSON.stringify(o2[index])) === -1) {
                tempNewObj.push(o2[index]);
            }
        });
    }

    //handling other types like string or number
    else {
        //taking value from the second object o2
        //could be modified to keep o1 value with tempNewObj = o1;
        tempNewObj = o2;
    }
    return tempNewObj;
};

演示復雜的對象。 我已經變成了展示jQuery的.extend()和我的腳本之間的差異的博客,留言本在這里

http://jsfiddle.net/J9EpT/

function merge(one, two){
  if (!one.data) return {data:two.data};
  if (!two.data) return {data:one.data};
  var final = {data:one.data};
  // merge
  for(var i = 0 ; i < two.data.length;i++){
      var item = two.data[i];
      insert(item, final);
  }
  return final;
}


function insert(item, obj){
    var data = obj.data;
    var insertIndex = data.length;
    for(var i = 0; i < data.length; i++){
        if(item.id == data[i].id){
           // ignore duplicates
           insertIndex = -1;
           break;
        } else if(item.id < data[i].id){
           insertIndex = i;
           break;
        }
    }
    if(insertIndex == data.length){
        data.push(item);
    } else if(insertIndex != -1) {
        data.splice(insertIndex,0,item);
    }
}

var final = merge(obj1, obj2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM