Posts

Showing posts from May, 2022

Add key-value pair to every object in array of objects in JavaScript

Image
Sometimes, we've to add flag or some new key-value pair to every object in array of objects. For that we can follow three ways to achieve it. For suppose, we have an array of objects as below and we want to add gender key to all the objects     students  = [     {        name:   'Radhika' ,        age:   26 ,     },     {        name:   'Radha' ,        age:   24 ,     },     {        name:   'Jyothi' ,        age:   25 ,     },   ]; // Expected Output    students  = [     {        name:   'Radhika' ,        age:   26 ,        gender:   'female'     },     {        name:   'Radha' ,        age:   24 ,        gender:   'female'     },     {        name:   'Jyothi' ,        age:   25 ,        gender:   'female'     },   ]; Method 1 - Using Map:       //Here we have to assign to a variable to get the modified output.    let   response  =  this . students . map (( student )  =>  ({... student , gender:   'fe

Avoiding multiple API calls in Angular by using shareReplay method of RxJS

Image
While working with some Angular projects, we've to subscribe the same api in to different methods in a component. In order to avoid that we can use shareReplay method of RxJS. Lets consider an app which has two buttons and two different methods gets called on clicking on those buttons. But, internally the two methods will subscribe the same api two times. Suppose user clicks the button multiple times, then we've to call the same api multiple times to display the number of completed/incomplete tasks. On clicking the `Completed Todos` and `Incomplete Todos` buttons, same API got two times. This will increase the load on server and increase the loading time of UI. Henceforth, reduces the performance of our application. We can avoid calling multiple API services with the help of shareReplay method of RxJS. shareReplay  subscribes the observable, caches the response and multicasts it to all the subscribers without calling the API multiple times. Lets see the above example with share

Extract list of values from an array of objects to an array based on a key in JavaScript.

Image
There may be a situation where you want to extract the list of values of a specific key from an objects to an array.  Method 1: In one way, we can use both for loop and Array.push() method and achieve it.  For example, we have an Array of objects as below let studentsObject = [     {         "name" : "John" ,         "age" : 26     },     {         "name" : "Helen" ,         "age" : 42     },     {         "name" : "Cole" ,         "age" : 54     },     {         "name" : "Noah" ,         "age" : 38     } ] we can extract the values as shown below  let namesList ; studentsObject . forEach ( student => {     namesList . push ( student . name ) }); console . log ( namesList ); //Output [ 'John' , 'Helen' , 'Cole' , 'Noah' ] Method 2: The simple and easy way is using map function as below let namesList = studentsObject .