我正在开发运行在安卓手机上的Ionic2应用程序。它不需要远程数据库。所以我决定使用本地存储"商店"。
我找到了一些文章并做了一些改进,如下所示。我成功地获得了所有值,但我想在某些条件下获得一些东西,例如 SQL 中的"where"语句。(getWorksOn(someDate({...}(
我搜索并阅读了30多篇热门文章,但我找不到路。如果有办法,请告诉我。
// work-service.ts
addWork(work){
this.storage.get('myWorks').then((data) => {
if(data != null){
data.push(work);
this.storage.set('myWorks', data);
} else {
let tempWorks = [];
tempWorks.push(work);
this.storage.set('myWorks', tempWorks);
}
});
}
getWorks(){
return this.storage.get('myWorks');
}
getWorksOn(someDate){
// I want to return works that worked on someDate
// Is there any way to get some value conditionally
return this.storage.get('myWorks');
}
// work-list.ts
works:any;
...
ngOnInit(){
this.workService.getWorks().then((data)=>{
this.works = data;
});
}
如果您使用的是 Lodash,那么您可以如下所示进行操作。
注意:您无法在local storage
内执行此操作。那你必须做这样的事情...
getWorksOn(someDate){
this.storage.get('myWorks').then((val) => {
let myworkArray = val; //firstly, fetched the data
let myworkArray2= _.filter(myworkArray , { 'date': someDate});//after that filter out according to your condition
});
}
OP使用map
的反馈和filter
array
的功能
this.workService.getWorks().then((works) => { this.works = works.filter(work
=> work.dateStr === this.dateStr ); });