我有一个应用程序,我可以在其中加载数据、存储数据,并在某些情况下重新加载数据。但是当我重新加载时,如果它们已经加载,我不想复制我的数据,并且,如果有的话,我确实想应用修改。
我存储对象的数据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 非常快,适用于完全匹配,但不是在修改对象时,它会创建一个新对象。
编辑我使用了这个: