Angular Code Optimized Functions

Introduction

 
In Angular mostly we are using for loop for finding values from an array list. When we have more lists it takes some time to find the list value. So we need to reduce time and performance to find the list values using the below functions.
 
The first one is finding a specific item in an array. This is a very common scenario. Let's say your user wants to view detailed information about a specific item in their exportlist. You need to grab the item from the exportlist array with a specific ID. It's common to go use a for loop to get this.
 
Something like this,
  1. let providerid = 5;  
  2. let foundedItem = null;  
  3. for (var i = 0; i < this.exportlist.length; i++) {  
  4.     if (this.exportlist[i].providerid == providerid) {  
  5.         foundedItem = this.exportlist[i].providerid;  
  6.     }  
  7. }  

Find() Function

 
Now, this is certainly a reasonable method to use to find this item. But let's use a better structure. The find() method is much more optimized for this scenario,
  1. let foundedItem = this.exportlist.find(x => {  
  2.     return x.providerid == providerid  
  3. });  
Which can even be shortened to this:
  1. let foundedItem=this.exportlist.find(x=>x.providerid == providerid);  
In the list, Find will return the first item that matches the query. Most of the time we want to use this when we need to find a specific item in an array.
 
Generally, this is used when we are finding an item by some unique criteria, like the id shown above.
 

Some() Function

 
Even though we can use this same find function to find an item by non-unique criteria, such as finding the first item that has the providerStatus flag, generally this use case is better served by a Some()function.
 
When we want to find an item by some non-unique criteria, like an out of stock item, what we are really trying to do is find out if any item in the list, In a for loop, this looks like this,
  1. let anyItemproviderStatus = false;  
  2. for (var i = 0; i < this.exportlist.length; i++) {  
  3.     if (this.exportlist[i].providerStatus) {  
  4.         anyItemproviderStatus = this.exportlist[i].providerStatus;  
  5.     }  
  6. }  
  7. if (anyItemproviderStatus) {  
  8.     //User need to do function logic  
  9. }  
Sure, this works, but in this case, what we really want is the some() function.
  1. if(this.exportlist.some(x=>x.providerStatus)){  
  2.    //User need to do function logic  
  3. }  
Some method returns true if any item in the array matches the given criteria. Otherwise, it returns false. The longhand of the above is the following,
  1. let anyItemOutOfStock =this.exportlist.some(x=>{  
  2.    return x. providerStatus;  
  3. }); 

Filter() Function

 
We can do this much easier with the filter option,
 
If we need to filter more then list value from the particular list we have to split out not match providerStatus value to filter the list.
  1. let foundedItem=this.exportlist.filter(x=>  
  2. x. providerStatus!= providerStatus);  
I hope this article is most helpful to you. Thanks.


Similar Articles