我有两个对象数组。 localDataArray
已经存储在我的应用程序中,remoteUpdateDataArray
来自后端。
var localDataArray = [
{ "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
{ "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"},
{ "date": "10/01/19", "category": "skate", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}
];
var remoteUpdateDataArray = [
{ "date": "12/01/19", "category": "surf", "hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"},
{ "date": "11/01/19", "category": "surf", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"},
{ "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
{ "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"},
{ "date": "10/01/19", "category": "skate", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}
];
我想从remoteUpdateDataArray
中删除所有重复的对象。每个对象的唯一标识符是哈希。
到目前为止,我有以下代码:
let hashValue = "54fd1711209fb1c0781092374132c66e79e2241b"
var filteredResult = remoteUpdateDataArray.filter(x => x.hash !== hashValue);
结果:
var filteredResult = [
{ "date": "12/01/19", "category": "surf", "hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"},
{ "date": "11/01/19", "category": "surf", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"},
{ "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
{ "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"}
];
我如何还设法摆脱阵列内的另一个对象(在这种情况下为两个重复的对象)?请记住,这些数组可能会变得很大。
答案 0 :(得分:3)
我会从您的第一个数组构建一个哈希列表(以保存迭代),然后只需使用include()进行过滤
const inLocalData = localDataArray.map(({hash: e}) => e);
const result = remoteUpdateDataArray.filter(({hash: e}) => ! inLocalData.includes(e));
console.log(result);
<script>
var localDataArray = [{
"date": "10/01/19",
"category": "surf",
"hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "54fd1711209fb1c0781092374132c66e79e2241b"
}
];
var remoteUpdateDataArray = [{
"date": "12/01/19",
"category": "surf",
"hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"
},
{
"date": "11/01/19",
"category": "surf",
"hash": "54fd1711209fb1c0781092374132c66e79e2241b"
},
{
"date": "10/01/19",
"category": "surf",
"hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "54fd1711209fb1c0781092374132c66e79e2241b"
}
];
</script>
答案 1 :(得分:1)
看来您需要缓存。将哈希存储到缓存中,并在U获取新数据时对其进行更新。
var localDataArray = [
{ "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
{ "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"},
{ "date": "10/01/19", "category": "skate", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}
];
var cache = {}
// The result, make a copy of local data at start
var filtered = [].concat(localDataArray)
// initialize the cache
localDataArray.forEach(item => {
cache[item.hash] = true
})
// -----------
var remoteUpdateDataArray = [
{ "date": "12/01/19", "category": "surf", "hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"},
{ "date": "11/01/19", "category": "surf", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"},
{ "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
{ "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"},
{ "date": "10/01/19", "category": "skate", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}
];
// filter duplicated items
remoteUpdateDataArray.forEach(item => {
// item exists
if(cache.hasOwnProperty(item.hash)) {
return
}
// append hash and item
cache[item.hash] = true
// just append the new data items
filtered.push(item)
})
答案 2 :(得分:1)
为什么不使用lodash的uniqBy?
首先,使用spred opertaor将2个数组连接起来(您可以详细了解here):
const newArray = [...localDataArray ,...remoteUpdateDataArray];
以及具有所有重复项的新数组
const filteredResult = _.uniqBy(newObject,'hash');
答案 3 :(得分:0)
您也可以使用此
var localDataArray = [{
"date": "10/01/19",
"category": "surf",
"hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "54fd1711209fb1c0781092374132c66e79e2241b"
}
];
var remoteUpdateDataArray = [{
"date": "12/01/19",
"category": "surf",
"hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"
},
{
"date": "11/01/19",
"category": "surf",
"hash": "54fd1711209fb1c0781092374132c66e79e2241b"
},
{
"date": "10/01/19",
"category": "surf",
"hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "54fd1711209fb1c0781092374132c66e79e2241b"
}
];
let data =[];
localDataArray.map(item=>{
data= remoteUpdateDataArray.find(el=>item.hash!=el.hash)
})
console.log(data)