Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 228595

How to nicely filter inside an array of object?

$
0
0

Hi,

I read in Ionic doc and on the web that you can easily filter an array by using filter(). Something like

return this.items.filter((v) => {
  if(v.toLowerCase().indexOf(q.toLowerCase()) >= 0) {
    return true;
  }
  return false;
})

But what if the "items" is an array of objects?

What I have done seems to work, but I thought it could be nicer than that:

let newList : IInfo[] = new Array<IInfo>;

for (let idx = 0; idx < this.myList.length; idx++) {
  if (myList[idx].id.indexOf(str) >= 0)
    newList.push(this.myList[idx]);
  else if (myList[idx].name.indexOf(str) >= 0)
    newList.push(this.myList[idx]);
  else if (myList[idx].position.indexOf(str) >= 0)
    newList.push(this.myList[idx]);
})

Do you have other better suggestion?


Viewing all articles
Browse latest Browse all 228595

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>