Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 478 of 840
Compress array to group consecutive elements JavaScript
We are given a string that contains some repeating words separated by dash (-) like this: const str = 'monday-sunday-tuesday-tuesday-sunday-sunday-monday-monday-monday'; console.log(str); monday-sunday-tuesday-tuesday-sunday-sunday-monday-monday-monday Our job is to write a function that returns an array of objects, where each object contains two properties: val (the word) and count (their consecutive appearance count). Expected Output Format For the above string, the compressed array should look like this: const arr = [{ val: 'monday', count: 1 }, { val: 'sunday', ...
Read MoreHow to group JSON data in JavaScript?
Grouping JSON data in JavaScript involves organizing objects by a common property. This is useful when you need to categorize data or create summary reports from complex datasets. Basic Grouping with for...in Loop The traditional approach uses a for...in loop to iterate through object keys and group items by a specific property: var details = { "1": { name: "John" }, "2": { name: "John" ...
Read MoreES6 Property Shorthands in JavaScript
ES6 introduced property shorthand syntax that simplifies object creation when the property name matches the variable name. Instead of writing name: name, you can simply write name. Syntax // ES5 way let obj = { property: property, anotherProperty: anotherProperty }; // ES6 shorthand let obj = { property, anotherProperty }; Basic Example ES6 Property Shorthands ...
Read MoreHow to find keys of a hash in JavaScript?
In JavaScript, objects act as hash tables where you can store key-value pairs. To extract all keys from an object, use the built-in Object.keys() method, which returns an array of the object's property names. Syntax Object.keys(object) Parameters object: The object whose keys you want to retrieve. Return Value Returns an array of strings representing the object's own enumerable property names (keys). Example Find Hash Keys ...
Read MoreJavaScript program to decrement a date by 1 day
JavaScript provides several methods to manipulate dates. To decrement a date by 1 day, we can use the setDate() and getDate() methods together. Syntax date.setDate(date.getDate() - 1); Parameters date.getDate() - Returns the current day of the month (1-31) date.setDate() - Sets the day of the month for the date object Example: Basic Date Decrement Decrement Date by 1 Day body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...
Read MoreImplementing Linear Search in JavaScript
Linear search is a simple algorithm that sequentially checks each element in an array until the target element is found or the end of the array is reached. It has a time complexity of O(n) in the worst case. How Linear Search Works Linear Search Process Array: [1, 19, 5, 11, 22, 55] Target: 22 1 i=0 19 i=1 ...
Read MoreRecursion in array to find odd numbers and push to new variable JavaScript
We are required to write a recursive function, say pushRecursively(), which takes in an array of numbers and returns an object containing odd and even properties where odd is an array of odd numbers from input array and even an array of even numbers from input array. This has to be done using recursion and no type of loop method should be used. Syntax const pushRecursively = (arr, len = 0, odd = [], even = []) => { // Base case: if we've processed all elements if (len ...
Read MoreHow to set text in tag with JavaScript?
In JavaScript, you can set text in HTML tags using various methods. This tutorial shows different approaches including jQuery and vanilla JavaScript. Using jQuery .html() Method First, create a element with an ID attribute: Replace This strong tag Then use jQuery to set the text content: $(document).ready(function(){ $("#strongDemo").html("Actual value of 5+10 is 15....."); }); Complete jQuery Example Set Text in Tag ...
Read MoreSum is to be calculated for the numbers in between the array's max and min value JavaScript
We need to write a function called sumBetween() that takes an array of two elements and returns the sum of all integers between those values, including both endpoints. For example: [4, 7] = 4+5+6+7 = 22 [10, 6] = 10+9+8+7+6 = 40 Understanding the Problem The function should work regardless of which number is larger. Whether we pass [4, 7] or [7, 4], we need to sum all integers from 4 to 7 inclusive. Solution Using Mathematical Formula We can use the mathematical formula for sum of consecutive integers: sum from 1 ...
Read MoreHow to convert string type value to array type in JavaScript?
Converting strings to arrays in JavaScript can be done using different methods depending on the string format. Here are the most common approaches. Using JSON.parse() for JSON Strings When you have a JSON-formatted string, use JSON.parse() to convert it to an array: var customerDetails = '[{"name": "John", "countryName": "US"}, {"name": "David", "countryName": "AUS"}, {"name": "Bob", "countryName": "UK"}]'; console.log("Original string:", customerDetails); console.log("Type:", typeof customerDetails); var convertStringToArray = JSON.parse(customerDetails); console.log("After converting to array:"); console.log(convertStringToArray); console.log("Type:", typeof convertStringToArray); Original string: [{"name": "John", "countryName": "US"}, {"name": "David", "countryName": "AUS"}, {"name": "Bob", "countryName": "UK"}] Type: string ...
Read More