Javascript 如何使用下划线+;ES 6
下面是我的两个数组Javascript 如何使用下划线+;ES 6,javascript,jquery,ecmascript-6,underscore.js,Javascript,Jquery,Ecmascript 6,Underscore.js,下面是我的两个数组 let clientCollection = ["1","ABC","X12","OE2","PQ$"]; let serverCollection = [{ "Id": "1", "Name": "Ram", "Other": "Other properties" }, { "Id": "ABC", "Name": "Shyam", "Other": "Other properties" }, { "Id":
let clientCollection = ["1","ABC","X12","OE2","PQ$"];
let serverCollection = [{
"Id": "1",
"Name": "Ram",
"Other": "Other properties"
},
{
"Id": "ABC",
"Name": "Shyam",
"Other": "Other properties"
},
{
"Id": "OE2",
"Name": "Mohan",
"Other": "Other properties"
}]
现在我需要比较以上两个集合并创建两个子数组
let matchedIds = [];
let unMatchedIds = [];
这就是我目前正在做的
for(let i =0 ; i < clientsCollection.length;i++)
{
if(_.indexOf(serverCollection, clientCollection[i]) >= 0)
{
matchedIds.push(clientCollection[i]);
}
else
{
unMatchedIds.push(clientCollection[i]);
}
}
for(设i=0;i=0)
{
matchedIds.push(clientCollection[i]);
}
其他的
{
不匹配的dids.push(clientCollection[i]);
}
}
在我的应用程序中,这些阵列的大小可以增加到1000或更多。这可能是效率问题
我正在使用下划线&如果我能找到更好的解决方案,我会尝试,但还没有找到
有人可以建议我是否可以使用下划线+ES6以更高效的方式执行相同的操作吗?创建一个服务器ID列表。用于迭代客户端,并根据子数组在服务器集合中的存在情况将其分配给子数组。使用将子数组提取到变量
const clientCollection=[“1”、“ABC”、“X12”、“OE2”、“PQ$”];
const serverCollection=[{“Id”:“1”,“Name”:“Ram”,“Other”:“Other properties”},{“Id”:“ABC”,“Name”:“Shyam”,“Other”:“Other properties”},{“Id”:“OE2”,“Name”:“Mohan”,“Other”:“Other properties”}];
//创建一组ID。可以使用下划线的Pull而不是map
const serverSet=new Set(serverCollection.map(({Id})=>Id));
//将ID减少为两个数组(matchedID、unmatchedID)的数组,然后使用解构赋值将其赋值给变量
const[matchedIds,unMatchedIds]=clientCollection.reduce((r,id)=>{
r[serverSet.has(id)?0:1].push(id);//根据集合中的存在推送到子数组
返回r;
}, [[], []])
console.log(matchedIds);
console.log(不匹配的DIDS)代码>我认为,这将是matchedds
人口的一个好方法:
for(let i = serverCollection.length - 1; i >= 0; i--) {
const id = serverCollection[i]['Id'];
if(clientCollection.indexOf(id) !== -1) {
matchedIds.push(id);
}
}
这一个用于matchedds
完成后的unmatcheddis
:
for (var i = clientCollection.length - 1; i >= 0; i--) {
if (matchedIds.indexOf(clientCollection[i]) === -1) {
unMatchedIds.push(clientCollection[i]);
}
}
filter
、reduce
等中没有一个比基本indexOf
更快
UPD
我创建了一个plunker:。他说,对于10000个项目,这个解决方案比这里建议的其他两个解决方案快5倍。我会从所有服务器ID中选择一个。然后循环查看id是否在集合中,并将其添加到两个数组中:
让服务器集合=[
{
Id:'1',
名称:“Ram”,
其他:“其他财产”
},
{
Id:‘ABC’,
姓名:‘Shyam’,
其他:“其他财产”
},
{
Id:‘OE2’,
姓名:'磨憨',
其他:“其他财产”
}
];
让clientCollection=['1','ABC','X12','OE2','PQ$'];
const serverIds=new Set(serverCollection.map((server)=>server.Id));
让matchedIds=[];
让不匹配的dids=[];
for(let客户端集合的id){
if(serverIds.has(id)){
matchedIds.push(id);
}否则{
不匹配的推送(id);
}
}
console.log('matched',matchedIds);
console.log('unmatched',unmatched)
您仍然在整个集合上循环,然后对每个项目执行indexOf
。比较应该在indexOf
和Set.has
之间,并且。只有当clientCollection
中的项目很少时,才是如此。看见随着收藏规模的增长,此解决方案将变得更慢。请在我的plunker上尝试10万或100万件物品,你会发现即使是你的新尝试也比我的慢2倍以上。哦,有意义!您增加了数组长度。。。是的,这是一个很好的观点,您没有将项目添加到集合中,您只是多次运行相同的几个项目。请看我的plnkr,它只增加了100个项目,它变得非常缓慢。您的解决方案是O(n*m)
,随着两个阵列的增长,速度将越来越慢。不幸的是,您的代码无法工作。它只填充matchedIds列表,但填充错误。我在控制台里试过?我看到您正在从serverCollection创建集合,但我想知道我们是否也从clientCollection创建集合??这就是clientCollection数组的构建方式let clientCollection=self.productIds.toString().trim().split(“\n”)代码>从clientCollection创建一个集合并没有真正的帮助,因为您需要遍历整个集合,无论如何,以数组形式遍历它们已经很有效了。如果必须从字符串中解析ProductID,那么在提高速度方面没有什么可以做的。好的。谢谢:)您的初始解决方案不起作用(由于.indexOf(serverCollection,clientCollection[i])
,填充是错误的),但是for
和下划线indexOf
的组合并不是那么糟糕。。。请看我的答案,它满足了这个问题,而且速度非常快。@dhilt,是的,我正在分析您的plunkr链接,随附感谢:)新信息已到达。增加集合大小会严重影响我当前的解决方案。我错了。你的serverCollection.map(({Id})=>Id)
等于serverCollection.map(x=>x.Id)
?确实如此。我发现分解可以减少噪音。