Author: Hiral Patel

Sort JSON by Key or Value in Javascript

JSON Object does not maintain order of insertion. This post discusses how to sort JSON object by key or value. In order to sort JSON objects, we have to take help of Arrays. Arrays maintain an order of insertion. Moreover, it provides native sort() method to sort array elements. Sort JSON Object by Key: Following sortByKey() function takes JSON Object as an input and returns JSON Array which is sorted by key. function sortByKey(jsObj){ var sortedArray = []; // Push each JSON Object entry in array by [key, value] for(var i in jsObj) { sortedArray.push([i, jsObj[i]]); } // Run...

Read More

Loop over Map Implementation in Java

This post discusses basic methods to loop over Map in Java. Using following methods, you can loop over any Map implementation such as HashMap, LinkedHashMap, TreeMap etc… Initialization Lets initialize a HashMap containing country code and country name as below. // Initialize Hashmap Map<String, String> countryCodes = new HashMap<String, String>(); // Load Hashmap countryCodes.put("AUS", "Australia"); countryCodes.put("CAN", "Canada"); countryCodes.put("PAK", "Pakistan"); countryCodes.put("INR", "India"); Method 1: Iterate and remove value from Map THIS IS THE ONLY METHOD WHICH CAN ITERATE OVER MAP AND REMOVE ELEMENT FROM UNDERLYING MAP INSTANCE WHILE ITERATION. All other methods can iterate over Map elements, but they does...

Read More

Why you should explicitly set cache = false in jQuery Ajax

Introduction AJAX is one of the best invention happened for user experience improvement. AJAX helps to dynamically manipulate content on user’s browser without reloading the webpage. Now days, it is very common to use AJAX in load/add/update/delete content from server to client’s machine. Scenario For all jQuery AJAX requests, default option is cache = true for all datatype except datatype = ‘script’ or ‘jsonp’. Different browser have different default policy on caching ajax requests. Consider below scenario to understand my point on why you should explicitly set cache=false in ajax request. Step 1: Make an AJAX request to get...

Read More

Configure JSHint in Sublime Text for Code Quality

JSHint is a tool for detecting potential Javascript errors and deviation from code standards. It is a very useful tool to enforce code conventions in a team. This post discusses how to configure JSHint in Sublime Text. You can use jsHint in the sublime text with following 2 plugins. jsHint plugin for showing build errors in a sublime text console. SublimeLinter-jsHint plugin for inline JS code linting. Install jsHint You can install jsHint via npm package manager. If you do not have package manager installed then go to Node.js Website and install Node on your operating system. This will...

Read More

Regular Expression Usage in Javascript

Regular expressions are used to find patterns in a string. Below are the common uses for regular expression in javascript. If you are new to regular expression, then check out introduction to regular expression in javascript post before continuing on this post. Following are the most common usage of regular expression in javascript. Use 1 : RegExp.exec() to find first matching string The regexObj.exec(str) method look for a first match in a specified string and returns result array of matching string. If no matching string found then returns null. If you want to get all matching string then use...

Read More