对合并数组进行重复数据删除

时间:2019-02-21 02:38:41

标签: javascript

我有一个应用程序,可以在其中加载数据,存储数据,并在某些情况下重新加载数据。 但是,当我重新加载数据时,如果数据已经加载,我不想复制它们,并且如果有的话,我也想应用修改。

我存储对象的数据2数组,看起来像

[{ 
    created_at: "2019-02-20T18:18:37.19+09:00"
    display_order: 0
    h_id: "5c6d1b6d3b6fe20690e2ef80"
    is_related_post: false
    item_id: "5c5a865e3b6fe24da8de7f21"
    user_id: "5c468bfa3b6fe24f78a36cdc"
    _id: "5c6d1b6d3b6fe20690e2ef80"
}]

代码如下

var data1 = [{
  created_at: "2019-02-20T18:18:37.19+09:00",
  value: "HELLO",
  h_id: "5c6d1b6d3b6fe20690e2ef80",
  is_related_post: false,
  item_id: "5c5a865e3b6fe24da8de7f21",
  user_id: "5c468bfa3b6fe24f78a36cdc",
  _id: "5c6d1b6d3b6fe20690e2ef80"
}, {
  created_at: "2019-02-15T14:45:29.17+09:00",
  value: "HELLO",
  h_id: "5c6651f93b6fe24e40e2b0e9",
  is_related_post: false,
  item_id: "5c5a865e3b6fe24da8de7f22",
  user_id: "5c468bfa3b6fe24f78a36cdc",
  _id: "5c6651f93b6fe24e40e2b0e9"
}]
var data1Copy = [{
  created_at: "2019-02-15T14:45:29.17+09:00",
  value: "HELLO",
  h_id: "5c6651f93b6fe24e40e2b0e9",
  is_related_post: false,
  item_id: "5c5a865e3b6fe24da8de7f22",
  user_id: "5c468bfa3b6fe24f78a36cdc",
  _id: "5c6651f93b6fe24e40e2b0e9"
}]

var dataValueChange = [{
  created_at: "2019-02-15T14:45:29.17+09:00",
  value: "MODIFIED HELLO",
  h_id: "5c6651f93b6fe24e40e2b0e9",
  is_related_post: false,
  item_id: "5c5a865e3b6fe24da8de7f22",
  user_id: "5c468bfa3b6fe24f78a36cdc",
  _id: "5c6651f93b6fe24e40e2b0e9"
}]
var dataNewId = [{
  created_at: "2019-02-15T14:45:29.17+09:00",
  value: "MODIFIED HELLO",
  h_id: "5c6651f93b6fe24e40e2b0e9333",
  is_related_post: false,
  item_id: "5c5a865e3b6fe24da8de7f22",
  user_id: "5c468bfa3b6fe24f78a36cdc",
  _id: "5c6651f93b6fe24e40e2b0e9"
}]

var testFunction = (name, func, optParams1, optParams2) => {
  var t0 = performance.now();
  var result = func(optParams1, optParams2);
  var t1 = performance.now();
  console.log("Call " + name + " took " + (t1 - t0) + " milliseconds.");
  console.log("result :", result);
}


var DeduplicateArray = (array) => {
  var hashTable = {};

  return array.filter(function(el) {
    var key = JSON.stringify(el);
    var match = Boolean(hashTable[key]);
    return (match ? false : hashTable[key] = true);
  });
}

var DeduplicateArray2 = (from, array) => {
  var newStory = [];
  array.forEach(history => {
    let isAlreadyStored = false;
    from.forEach(storedHistory => {
      if (isAlreadyStored) return;
      if (history.h_id === storedHistory.h_id) {
        isAlreadyStored = true;
      }
    });
    if (!isAlreadyStored) {
      newStory.push(history);
    };
  });
  return newStory;
}

console.log("SHOULD RETURN THE DATA NOT DUPLICATED");
testFunction("sameObject1", DeduplicateArray, data1.concat(data1Copy));
testFunction("sameObject2", DeduplicateArray2, data1, data1.concat(data1Copy));
console.log("");
console.log("");

console.log("THIS SHOULD RETURN THE DATA NOT DUPLICATED, BUT MODIFIED");
testFunction("valueChange1", DeduplicateArray, data1.concat(dataValueChange));
testFunction("valueChange2", DeduplicateArray2, data1, data1.concat(dataValueChange));
console.log("");
console.log("");

console.log("THIS SHOULD ADD A DATA TO THE LIST");
testFunction("idChange1", DeduplicateArray, data1.concat(dataNewId));
testFunction("idChange2", DeduplicateArray2, data1, data1.concat(dataNewId));
console.log("");
console.log("");

https://codepen.io/anon/pen/KJYEOg?editors=0010

我的两个功能有问题:

如果我有大量数据,则forloop的执行时间很长,并且不返回修改。如果有办法使嵌套的forloop更快,我会用它,因为我可以用另一种方法进行更改。

stringify非常快速,可以完全匹配,但是修改对象时不会,它将创建一个新对象。

我使用此编辑:

Converting lodash _.uniqBy() to native javascript

2 个答案:

答案 0 :(得分:1)

如果您准备使用库,我建议使用lodash的_uniqBy函数。这很简单:

let unique = _.uniqBy(data, 'h_id');

您可以放心,lodash的实现将接近最佳。

答案 1 :(得分:1)

这是合并数组的简单示例

var array1 = ["Shohel","Rana"];
var array2 = ["Shipon", "Shohel"];
let noDuplicate = array1.filter ( i => array2.findIndex( a => i==a)==-1 );
let result = [...noDuplicate, ...array2];
console.log(result);

您的代码可以通过这种方式编写

console.log("SHOULD RETURN THE DATA NOT DUPLICATED");
let noDuplicate = data1.filter ( i => data1Copy.findIndex(a => i.h_id==a.h_id)==-1 );
console.log(noDuplicate);