Ionic 2 - 如何将大型 JSON 数据放入变量中并无延迟地删除重复项?
Ionic 2 - How to put a large JSON data in variable and remove the duplicates without lag?
我有一个包含超过 40K (40000) 行的 JSON 文件。
我用这段代码获取了json的数据,并将接收到的数据放在变量中:
this.http.get(url).map(res => res.json()).subscribe(data => {
...
this.users=data.users;
...
this.images=data.images;
this.removeDupicates();
...
});
我的问题:加载 JSON 的数据并尝试将数据放入变量并尝试删除重复项时,应用程序需要很长时间才能准备就绪。
那么是否有任何解决方案可以加快或修复此过程?
编辑:
这里是函数的代码"removeDuplicates"
removeDupicates(){
for(let i=0; i<this.images.length; i++){
for(let j=0; j<this.images.length; j++){
if(this.images[i].img == this.images[j].img && i!=j){
this.images.splice(i,1);
}
}
}
}
在 中找到并根据您的情况进行了修改:
Array.from(new Set(
[
{"img":"1"},
{"img":"2"},
{"img":"1"},
{"img":"3"}
].map((itemInArray) => itemInArray.img)));
输出将是["1,"2","3"]
现在,如果您希望它异步发生,这样就不必在继续应用程序之前等待数据加载,您可以将其包装在 setTimeout
中
所以:
removeDuplicates() {
setTimeout(() => {
this.images = Array.from(new Set(
this.images.map(image => image.img)
));
}, 0); // execute timeout function immediately, fakes async
}
请注意,您不会有像这样的列表:[{img: 'myimage'}, ....]
但您现在有一个列表 ['myimage', 'mysecondimage', ...]
我有一个包含超过 40K (40000) 行的 JSON 文件。
我用这段代码获取了json的数据,并将接收到的数据放在变量中:
this.http.get(url).map(res => res.json()).subscribe(data => {
...
this.users=data.users;
...
this.images=data.images;
this.removeDupicates();
...
});
我的问题:加载 JSON 的数据并尝试将数据放入变量并尝试删除重复项时,应用程序需要很长时间才能准备就绪。
那么是否有任何解决方案可以加快或修复此过程?
编辑:
这里是函数的代码"removeDuplicates"
removeDupicates(){
for(let i=0; i<this.images.length; i++){
for(let j=0; j<this.images.length; j++){
if(this.images[i].img == this.images[j].img && i!=j){
this.images.splice(i,1);
}
}
}
}
在
Array.from(new Set( [ {"img":"1"}, {"img":"2"}, {"img":"1"}, {"img":"3"} ].map((itemInArray) => itemInArray.img)));
输出将是["1,"2","3"]
现在,如果您希望它异步发生,这样就不必在继续应用程序之前等待数据加载,您可以将其包装在 setTimeout
所以:
removeDuplicates() {
setTimeout(() => {
this.images = Array.from(new Set(
this.images.map(image => image.img)
));
}, 0); // execute timeout function immediately, fakes async
}
请注意,您不会有像这样的列表:[{img: 'myimage'}, ....]
但您现在有一个列表 ['myimage', 'mysecondimage', ...]