Month: March 2021

How to handle JSON data in JavaScript

 JSON (JavaScript Object Notation) is a way to represent data through text. It exists as a String and it supports all the basic data types that are supported by a standard JavaScript object. As per Wikipedia, the definition goes -JSON (JavaScript Object Notation, pronounced /ˈdʒeɪsən/; also /ˈdʒeɪˌsɒn/) is an open standard file format, and data interchange format, that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value).Reference: https://en.wikipedia.org/wiki/JSONNote – I am putting my JavaScript Developer study notes here. There are two methods that I will be discussing in this post. JSON.parse()This method is used to deserialize or convert a JSON string into a JavaScript object. This is highly used while accessing the external API/server response. In the below example, I am converting a string to a JavaScript object using the parse method. You can use the dot (.) to access the individual items in the object.Code:const jsonString = ` { “name”:”Sudipta Deb”, “country”:”Canada”, “favouriteCars”:[ “Honda”,”BMW” ], “employed”:true } `;const jsonObject = JSON.parse(jsonString);console.log(jsonObject);console.log(jsonObject.name);Output:{ name: ‘Sudipta Deb’, country: ‘Canada’, favouriteCars: [ ‘Honda’, ‘BMW’ ], employed: true}Sudipta DebJSON.stringify() This method is used to serialize or convert a JavaScript object into a JSON string. This is highly used before transferring data to an external API/server. In the below example, I am converting the above JavaScript object to a string using the stringify method. Code:const jsonString = ` { “name”:”Sudipta Deb”, “country”:”Canada”, “favouriteCars”:[ “Honda”,”BMW” ], “employed”:true } `;const jsonObject...

Read More

Sorting Arrays in JavaScript

 Like any programming language, JavaScript also provides ways to sort arrays. Different JavaScript engines implement this method using different sort algorithms. For example -Safari: Quicksort, Merge Sort, or Selection SortV8: Quicksort, or Insertion SortFirefox: Merge SortBut in reality, it doesn’t really matter much as long as the arrays are getting sorted.Note – I am putting my JavaScript Developer study notes here. The prototype for Sort method available in Array class is:Array.sort([compareFunction])Normally a callback function is passed which makes the sorting logic i.e. how the two numbers will be compared. This callback function should take two parameters i.e. param1 and param2 and works like this -callback function will return zero when both param1 and param2 are treated as equalcallback function will return 1 when both param1 is smaller than param2 callback function will return 11 when both param2 is smaller than param1Note – In default sorting, param1 and param2 are always converted into Strings and then they will be sorted alphabetically.For example, if we consider the below examplelet numbers = [100, 23, 45, 12, 99];let sortedNumbers = numbers.sort();console.log(sortedNumbers);Output: [ 100, 12, 23, 45, 99 ]But that’s not the correct sorting. The reason behind is that numbers here are converted into Strings and then sorted alphabetically due to default sorting.To solve this we need to pass the callback function which will define the comparison logic. Below is the modified code -let numbers = [100, 23, 45, 12, 99];let...

Read More

Kitchener Canada Developer Group Event: Let’s Learn About Heroku and How to Integrate with Salesforce by Julian Duque

Special thanks to our speaker, Julian Duque, for the session on “Let’s Learn about Heroku and How to Integrate with Salesforce”.You can register for all the upcoming sessions from the Kitchener Developer Group here.You can get details (presentation and recording) of all the earlier sessions from the Kitchener Developer Group here.Presentation: Let’s Learn About Heroku and How to Integrate with Salesforce from Sudipta Deb ☁...

Read More

Kitchener Canada Developer Group Event: Using Styling Hooks to Customize Your LWC by Kirupa Chinnathambi

Special thanks to our speaker, Kirupa Chinnathambi, for the session on “Using Styling Hooks to Customize Your LWC”.You can register for all the upcoming sessions from the Kitchener Developer Group here.You can get details (presentation and recording) of all the earlier sessions from the Kitchener Developer Group here.Presentation: Using Styling Hooks to Customize Your LWC from Sudipta Deb ☁...

Read More

Archives