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 261 of 534
JavaScript Count the number of unique elements in an array of objects by an object property
In JavaScript, when dealing with arrays of objects, it's common to need to extract specific values based on object properties and count how many unique occurrences exist for a particular property. Whether you're processing a list of users, products, or any other data structure, counting unique elements by a property is a useful operation. In this article, we'll discuss how to count the number of unique elements in an array of objects based on a specific property using a few different techniques. Problem Statement You have an array of objects representing users, each with properties like name, ...
Read MoreJavaScript: How to Check if a String is a Literal or an Object?
In this article, we will explore how JavaScript strings can exist as literals or objects, and learn methods to distinguish between them. In JavaScript, strings can exist in two forms: String Literal: Created using quotes (', ", or `) String Object: Created explicitly using the String constructor with new String("text") Understanding String Literals vs String Objects String literals are primitive values, while String objects are wrapper objects around the primitive string value. This distinction affects how they behave with certain operators and methods. Using typeof to Check String ...
Read MoreGenerate all combinations of supplied words in JavaScript
In JavaScript, there are scenarios where you may need to generate every possible combination of a string's characters. This can be especially useful in areas like cryptography, analyzing subsets of data, or solving problems involving permutations. In this article, we'll learn to implement a JavaScript function to achieve this task. Generate all combinations of supplied words Following are the different approaches for generating all combinations of supplied words − Recursive Approach Iterative Approach Using Bitmasking Using Recursive Approach The recursive approach builds combinations by including ...
Read MoreJavaScript - Find if string is a palindrome (Check for punctuation)
In JavaScript, checking if a string is a palindrome while handling punctuation requires cleaning the string first. A palindrome reads the same forwards and backwards, ignoring spaces, punctuation, and case. What is a Palindrome? A palindrome is not limited to single words like "radar" or "level"; it can include phrases or sequences like "Madam, in Eden, I'm Adam." We need to ignore punctuation, spaces, and case differences to ensure accurate palindrome checks. Using String and Array Methods The first approach uses JavaScript's built-in string and array methods to normalize and reverse the string: ...
Read MoreJavaScript - Finding the third maximum number in an array
In this article, we will learn to find the third maximum unique number in an array of JavaScript, or the largest number if there are fewer than three unique values. We will be using three methods: a single-pass approach for efficiency, a sorting method to identify the number after removing duplicates, and a combination of Set and Max-Heap for optimal handling of large datasets. Problem Statement Given an array of integers, find the third maximum unique number. If the array contains fewer than three unique numbers, return the largest number in the array. Input: const nums = ...
Read MoreJavaScript - Complementary Colors Builder
In this article, we will learn to build a JavaScript function that calculates the complementary color for a given hex color code. In web development, working with colors and their relationships is an essential aspect of design and user interface. One of the concepts that designers often use is complementary colors—colors that are opposite each other on the color wheel, providing contrast and enhancing visual appeal. You can try our Online Color Picker Tool to create new colors. Understanding Complementary Colors The following concepts help us understand complementary colors: Complementary Colors: ...
Read MoreJavaScript - array undefined element count
In this article, we will learn to count the number of undefined elements in an array using JavaScript. When working with arrays in JavaScript, you may encounter situations where certain elements are either explicitly set as undefined or are missing altogether (often represented by empty slots in sparse arrays). Problem Statement Given an array that may contain undefined values, we need to count how many elements in the array are defined (i.e., not undefined). Input const arr = [12, undefined, "blabla", , true, 44]; Output 4 4 elements are ...
Read MoreJavaScript: take every nth Element of Array and display a fixed number of values?
In this article, we will learn to extract every nth element from an array and display a fixed number of values in Javascript. The objective is to filter out every second element (or more generally every nth element) from an array and limit the result to a specified count of elements. We will explore different approaches in JavaScript for achieving this functionality, each offering a unique way to solve the problem. Problem Statement Let's say you have an array that takes every nth element. Display a fixed number of values after extracting the elements. Input Let's say the ...
Read MoreJavaScript function to prepend string into all the values of array?
In this article, we will learn to prepend a string into all the values of an array in JavaScript. Arrays are versatile data structures used to store multiple elements of similar or mixed types. A common operation is prepending a string to each element of an array, which involves adding a specified string at the beginning of each element. Problem Statement Given a string to prepend and an array of values, we need to add the string at the beginning of each array element and return the modified array. Example Input: const str = "Hello"; ...
Read MoreJavaScript Quicksort Recursive
In this article, we will learn to implement Quicksort recursively in JavaScript. Quicksort is one of the most efficient and widely used sorting algorithms, known for its divide-and-conquer approach. What is Quicksort? Quicksort is a divide-and-conquer sorting algorithm that sorts an array by selecting a pivot element and partitioning the array into two subarrays: elements smaller than the pivot and elements greater than or equal to the pivot. It then recursively sorts the subarrays. Quicksort Process ...
Read More