Posts

Showing posts from November 8, 2020

Create Custom Form Control for ng-select in Angular

Image
How to Create Custom Form Control in Angular  Why we create custom form control ? Normally, we used to create custom form control for re-usability.  The best advantage is that to add common functionality and features in this custom form control and extend the functionality as per our need and requirement.  And we can use everywhere into the application. But this example will tell you how to create custom form control to extend the functionality of  angular ng-select component.  We are adding a feature for this control, multi selection along with checkbox. Before that, we need to know what is the basic syntax of creating custom form control in angular. So, we start by implementing the ControlValueAccessor interface from @angular/forms into the component. This interface has three functions that need to be implemented and one optional functional. writeValue(obj: any): void { } registerOnChange(fn: any): void { } registerOnTouched(f

Create a function to convert from string to Decimal using JavaScript

Create a function to convert a string to decimal using JavaScript. Javascript function parseStringToDecimal(inputValue: string): Number { let value: number; if (inputValue === '0.00' || Number(inputValue) === 0.00) { value = 0.00; } else if (inputValue === '') { value = null; } else { value = parseFloat(inputValue.toString().replace(/,/, '')); } return value; } Expected Result :  Example const value = parseStringToDecimal("1,000.256"); console.log(value); Output :  1000.256

Create function to convert from string to Integer using javascript

Create a function to convert a string to integer using JavaScript. Javascript function parseStringToInteger(inputValue: string): Number { let value: number; if (inputValue === '0' || Number(inputValue) === 0) { value = 0; } else if (inputValue === '') { value = null; } else { value = Number(inputValue); } return value; } How to use the above function to get the expected result :  Javascript const value = parseStringToInteger("25"); console.log(value); Output : 25

Remove duplicate objects/data from an array of objects in Javascript

How to Remove duplicate object data from an array using JavaScript. To remove the duplicate object data from an array, have to use the filter function. So, it will filter out the duplicate objects/data from an array. Please see the below example. Javascript var uniqueItems = []; var sampleData = [ { id: 1, name: 'firstname' }, { id: 2, name: 'middlename' }, { id: 1, name: 'firstname' }, { id: 1, name: 'lastname' } ] var duplicateItems = sampleData.filter(filteredData => { if (uniqueItems.find(items => items.id === filteredData.id)) { return true; } uniqueItems.push(filteredData); return false; });