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
Javascript Articles
Page 121 of 534
Sort in multi-dimensional arrays in JavaScript
Sorting multi-dimensional arrays in JavaScript requires handling each subarray while maintaining specific sorting rules. This article demonstrates how to sort elements within subarrays based on custom conditions. Problem Statement Suppose we have the following array of arrays: const arr = [ ["A", "F", "A", "H", "F", "F"], ["F", "A", "A", "F", "F", "H"] ]; We need to write a JavaScript function that sorts all subarrays according to these rules: If the elements are not either "A" or "F", they should maintain their position If the element is either ...
Read MoreIs uppercase used correctly JavaScript
In JavaScript, we can determine whether a string follows correct uppercase usage based on three specific rules. A string is considered to have correct uppercase usage if it meets any of these criteria: All letters in a word are capitals, like "INDIA". All letters in a word are not capitals, like "example". Only the first letter in a word is capital, like "Ramesh". We need to write a JavaScript function that takes a string and determines whether it complies with any of these three ...
Read MoreImplementing binary search in JavaScript to return the index if the searched number exist
We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a search number as the second argument. If the search number exists in the array, we need to return its index in the array, otherwise we need to return -1. We have to do this making use of the binary search algorithm. The binary search algorithm is basically a divide and conquer algorithm which recursively divides the array into halves until it converges to a singleton element. The sorting of array is necessary for binary search ...
Read MoreIs the second string a rotated version of the first string JavaScript
We are required to write a JavaScript function that takes in two strings, say str1 and str2. We are required to determine whether or not the second string is a rotated version of the first string. For example − If the input strings are − const str1 = 'abcde'; const str2 = 'cdeab'; Then the output should be true because str2 is indeed made by shifting 'ab' to the end of string in str1. Understanding String Rotation A string rotation means taking some characters from the beginning of a string and moving them ...
Read MoreMerge JavaScript objects with the same key value and count them
Suppose we have an array of objects like this: const arr = [{ "value": 10, "id": "111", "name": "BlackCat", }, { "value": 10, "id": "111", "name": "BlackCat", }, { "value": 15, "id": "777", "name": "WhiteCat", }]; We are required to write a JavaScript function that takes in one such array. The function should then merge all those objects together that ...
Read MoreMerge JSON array date based JavaScript
When working with JSON arrays containing objects with date properties, you often need to merge objects that share the same date. This is common when combining data from different sources or consolidating time-series data. Suppose we have the following array of objects: const arr = [ { "date": "2010-01-01", "price": 30 }, { "date": "2010-02-01", ...
Read MoreSort array according to the date property of the objects JavaScript
In JavaScript, sorting an array of objects by date requires converting date strings to Date objects for proper comparison. The Array.sort() method with a custom comparator function handles this efficiently. Sample Data Consider an array of objects with date properties: const arr = [ {date: "2016-06-08 18:10:00"}, {date: "2016-04-26 20:01:00"}, {date: "2017-02-06 14:38:00"}, {date: "2017-01-18 17:30:21"}, {date: "2017-01-18 17:24:00"} ]; Using Array.sort() with Date Comparison The most straightforward approach uses Array.sort() with a ...
Read MoreDynamic Programming: Is second string subsequence of first JavaScript
We are given two strings str1 and str2, we are required to write a function that checks if str1 is a subsequence of str2. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. For example, "ace" is a subsequence of "abcde" while "aec" is not because the relative order is not maintained. Understanding Subsequences In a subsequence, characters must appear in the same order as in the original string, but they ...
Read MoreTransforming array to object JavaScript
Suppose we have an array of strings like this − const arr = [ 'type=A', 'day=45' ]; We are required to write a JavaScript function that takes in one such array. The function should construct an object based on this array. The object should contain a key/value pair for each string in the array. For any string, the part before '=' becomes the key and the part after it becomes the value. Method 1: Using for Loop const arr = [ 'type=A', 'day=45' ]; const arrayToObject = (arr = []) => ...
Read MoreHow to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?
When working with flattened objects that use dot notation (like "car.make" or "visits.0.date"), you often need to convert them back into nested objects and arrays. This process is called "unflattening" and is useful when working with form data, APIs, or database results. The Problem Suppose we have a flattened object like this: const obj = { "firstName": "John", "lastName": "Green", "car.make": "Honda", "car.model": "Civic", "car.revisions.0.miles": 10150, "car.revisions.0.code": "REV01", ...
Read More