Remove First Element From Array or Shift Array using JavaScript.
You can use JavaScript array shift method to remove the first element from the array.
let arr = [1,2,3,4,5] let removed_elem = arr.shift(); console.log('Modified array is ', arr); console.log('Removed elem is ', removed_elem); // - "Modified array is ", [2, 3, 4, 5] // - "Removed elem is ", 1 shift method shifts elements to the left and returns the first element.
[Read More]
Skip For Loop Iteration | JavaScript
How to skip a for loop iteration in JavaScript?
You can use continue to skip a for loop iteration in JavaScript. Let’s see using an example. Here is the code to get sum of even numbers.
let count = 0; for(let i = 0; i < 10; i++){ if(i % 2 == 0) count+=i; } console.log('count is ', count); // count is 20 Now let’s say you don’t want 4 to be included in sum.
[Read More]
Stop For Loop | JavaScript
How to stop for loop in JavaScript?
You can use break to exit for loop in JavaScript. Here is the code to get sum of even numbers.
let count = 0; for(let i = 0; i < 10; i++){ if(i % 2 == 0) count+=i; } console.log('count is ', count); // count is 20 Here is how you can stop the loop, the moment 6 is encountered.
let count = 0; for(let i = 0; i < 10; i++){ if(i == 6) break; if(i % 2 == 0) count+=i; } console.
[Read More]
Unit Testing PrimeNG Confirm Dialog | Karma | Jasmine
How to unit test PrimeNG ConfirmDialog ConfirmationService.
Let’s say you are using the PrimeNG confirmDialog. Here is how your app.component.html looks:
<p-confirmDialog header="Confirmation" icon="pi pi-exclamation-triangle"></p-confirmDialog> <button (click)="confirm()" pButton icon="pi pi-check" label="Confirm"></button> Here is the app.component.ts file.
import { Component, OnInit } from '@angular/core'; import { ConfirmationService } from 'primeng/api'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], providers: [ConfirmationService] }) export class AppComponent implements OnInit { public confirmSuccess: any; public confirmReject: any; constructor(private confirmationService: ConfirmationService) { } ngOnInit() { } confirm() { this.
[Read More]
Unit Testing Promise.all in Angular | Karma | Jasmine
Unit Testing Promise.all in Angular. Unit testing asynchronous code in Angular.
Let’s say, this is your component code,
import { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { CommonService } from './common.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { public firstAPIresponse:any; public secondAPIresponse:any; public thirdAPIresponse:any; public errorMessage:any; constructor(private http : HttpClient, private service : CommonService){} ngOnInit(){ this.makeAPICall(); } makeAPICall(){ Promise.
[Read More]
UnShift Array | Add Element to Start of Array | JavaScript
Add Element to Beginning of Array or UnShift Array using JavaScript.
You can use JavaScript array unshift method to add element to the beginning of the array.
let arr = [1,2,3,4,5]; let modified_array_len = arr.unshift(6); console.log('Modified array is ', arr); console.log('Modifed array len is ', modified_array_len); // - "Modified array is ", [6, 1, 2, 3, 4, 5] // - "Modifed array len is ", 6 unshift method moves elements to the right of the array after adding new elements to start of the array.
[Read More]
Accessing Data From JSON | JavaScript
How to access data from JSON object array using JavaScript ?
Assuming you have the following JSON object,
[{ "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496" } }, "alternateEmail": ["ja3dec@gmail.com", "rpdddy57@gmail.com"] }, { "id": 2, "name": "Leanne", "username": "Samson", "email": "abc@april.biz", "address": { "street": "Muines Light", "suite": "Apt. 556", "city": "dert", "zipcode": "92998-3874", "geo": { "lat": "-37.
[Read More]
Angular Material Select With Search
How to Use Angular Material Select With Search ?
To get started, you need to install Angular material to your Angular app. Once you have Angular material installed, you need to import MatSelectModule and use Material Select in your app.
Here is your app.component.html:
<mat-select placeholder="Select price" [(ngModel)]="selectedValue" name="item"> <mat-option *ngFor="let item of items" [value]="item.value"> {{item.viewValue}} </mat-option> </mat-select> Import MatInput Module Now let’s add a search input to the HTML code.
[Read More]
Check if An Array is Sorted | JavaScript
Check If An Array is Sorted using JavaScript
Logic : You can take the first item and second item and subtract the value. If second item minus the first item is positive, they are sorted. Now you can can move the index forward and check the next two and similarly.
Here is the JavaScript implementation to check is an Array is sorted.
function sorted(arr){ let second_index; for(let first_index = 0; first_index < arr.
[Read More]
Check if JSON Property Exists | JavaScript
Check if JSON property exist using JavaScript.
You need to check if a JSON property exists, before you use it. For, example,
let obj = { keys: [1, 2, 3], empInfo: { firstName: 'jay', lastName: 'raj', age: 10 } }; In the above JSON you can access firstName as
obj.empInfo.firstName But what if empInfo doesn’t exist. You get an error. So, you need to check if empInfo exists.
Using hasOwnProperty You can do so using hasOwnProperty.
[Read More]