Javascript 使用多个值筛选对象数组
我有两个对象数组。虽然Javascript 使用多个值筛选对象数组,javascript,Javascript,我有两个对象数组。虽然localDataArray已存储在我的应用程序中,但remoteUpdateDataArray来自后端 var localDataArray = [ { "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"}, { "date": "10/01/19", "category": "skate", "hash": "a0f14
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"}
];
如何处理阵列中的其他对象(在本例中为两个重复对象)?请记住,这些数组可能会变得非常大。我会从第一个数组中构建一个哈希列表(以节省迭代次数),然后使用includes()进行简单过滤
const inLocalData=localDataArray.map(({hash:e})=>e);
const result=remoteUpdateDataArray.filter({hash:e})=>!inLocalData.includes(e));
控制台日志(结果)代码>
var localDataArray=[{
“日期”:“19年1月10日”,
“类别”:“冲浪”,
“哈希”:“da39a3ee5e6b4b0d3255bfef95601890afd80709”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“a0f1490a20d0211c997b44bc357e1972deab8ae3”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
}
];
var remoteUpdateDataArray=[{
“日期”:“19年1月12日”,
“类别”:“冲浪”,
“哈希”:“4A0A19218E082A343A1B17E533409AF9D98F0F5”
},
{
“日期”:“19年1月11日”,
“类别”:“冲浪”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
},
{
“日期”:“19年1月10日”,
“类别”:“冲浪”,
“哈希”:“da39a3ee5e6b4b0d3255bfef95601890afd80709”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“a0f1490a20d0211c997b44bc357e1972deab8ae3”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
}
];
我会从第一个数组中构建一个哈希列表(以节省迭代次数),然后使用includes()进行简单过滤
const inLocalData=localDataArray.map(({hash:e})=>e);
const result=remoteUpdateDataArray.filter({hash:e})=>!inLocalData.includes(e));
控制台日志(结果)代码>
var localDataArray=[{
“日期”:“19年1月10日”,
“类别”:“冲浪”,
“哈希”:“da39a3ee5e6b4b0d3255bfef95601890afd80709”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“a0f1490a20d0211c997b44bc357e1972deab8ae3”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
}
];
var remoteUpdateDataArray=[{
“日期”:“19年1月12日”,
“类别”:“冲浪”,
“哈希”:“4A0A19218E082A343A1B17E533409AF9D98F0F5”
},
{
“日期”:“19年1月11日”,
“类别”:“冲浪”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
},
{
“日期”:“19年1月10日”,
“类别”:“冲浪”,
“哈希”:“da39a3ee5e6b4b0d3255bfef95601890afd80709”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“a0f1490a20d0211c997b44bc357e1972deab8ae3”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
}
];
看来你需要一个缓存。将散列存储到缓存中,并在获取新数据时进行更新
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)
})
看来你需要一个缓存。将散列存储到缓存中,并在获取新数据时进行更新
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)
})
为什么不使用lodash
首先,使用spred opertaor连接2个阵列(您可以阅读更多相关内容):
当你有了新的阵列和所有的复制品
const filteredResult = _.uniqBy(newObject,'hash');
为什么不使用lodash
首先,使用spred opertaor连接2个阵列(您可以阅读更多相关内容):
当你有了新的阵列和所有的复制品
const filteredResult = _.uniqBy(newObject,'hash');
你也可以用这个
var localDataArray=[{
“日期”:“19年1月10日”,
“类别”:“冲浪”,
“哈希”:“da39a3ee5e6b4b0d3255bfef95601890afd80709”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“a0f1490a20d0211c997b44bc357e1972deab8ae3”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
}
];
var remoteUpdateDataArray=[{
“日期”:“19年1月12日”,
“类别”:“冲浪”,
“哈希”:“4A0A19218E082A343A1B17E533409AF9D98F0F5”
},
{
“日期”:“19年1月11日”,
“类别”:“冲浪”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
},
{
“日期”:“19年1月10日”,
“类别”:“冲浪”,
“哈希”:“da39a3ee5e6b4b0d3255bfef95601890afd80709”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“a0f1490a20d0211c997b44bc357e1972deab8ae3”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
}
];
让数据=[];
localDataArray.map(项=>{
data=remoteUpdateDataArray.find(el=>item.hash!=el.hash)
})
console.log(数据)
您也可以使用它
var localDataArray=[{
“日期”:“19年1月10日”,
“类别”:“冲浪”,
“哈希”:“da39a3ee5e6b4b0d3255bfef95601890afd80709”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“a0f1490a20d0211c997b44bc357e1972deab8ae3”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
}
];
var remoteUpdateDataArray=[{
“日期”:“19年1月12日”,
“类别”:“冲浪”,
“哈希”:“4A0A19218E082A343A1B17E533409AF9D98F0F5”
},
{
“日期”:“19年1月11日”,
“类别”:“冲浪”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
},
{
“日期”:“19年1月10日”,
“类别”:“冲浪”,
“哈希”:“da39a3ee5e6b4b0d3255bfef95601890afd80709”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“a0f1490a20d0211c997b44bc357e1972deab8ae3”
},
{
“日期”:“19年1月10日”,
“类别”:“滑板”,
“哈希”:“54fd1711209fb1c0781092374132c66e79e2241b”
}
];
让数据=[];
localDataArray.map(项=>{
data=remoteUpdateDataArray.find(el=>item.hash!=el.hash)
})
console.log(data)
如果它实际上与您的问题无关,为什么要提到localDataArray
?为什么要提到