Web Development Articles

Page 172 of 801

How to sort array by first item in subarray - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 586 Views

In JavaScript, you can sort an array of subarrays based on the first element of each subarray using the sort() method with a custom comparison function. The Problem Consider an array where each element is itself an array, and you want to sort by the first item in each subarray: var studentDetails = [ [89, "John"], [78, "Mary"], [94, "Alice"], [47, "Bob"], [33, "Carol"] ]; Sorting in Descending Order To sort by the ...

Read More

How to decrease size of a string by using preceding numbers - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 150 Views

Let's say our original string is the following with repeated letters − var values = "DDAAVIDMMMILLERRRRR"; We want to remove the repeated letters and precede letters with numbers. For this, use replace() along with regular expression. Syntax string.replace(/(.)\1+/g, match => match.length + match[0]) How It Works The regular expression /(.)\1+/g matches any character followed by one or more repetitions of the same character. The replacement function returns the count plus the original character. Example Following is the code − var values = "DDAAVIDMMMILLERRRRR"; var precedingNumbersInString = ...

Read More

ES6 Default Parameters in nested objects – JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 631 Views

ES6 allows you to set default parameters for nested objects using destructuring assignment. This feature helps handle cases where nested properties might be undefined or missing. Syntax function myFunction({ outerKey: { innerProperty = defaultValue, anotherProperty = anotherDefault } = {} } = {}) { // Function body } Example Here's how to implement default parameters in nested objects: function ...

Read More

Replace multiple instances of text surrounded by specific characters in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 364 Views

Let's say we have a string where certain text is surrounded by special characters like hash (#). We need to replace these placeholders with actual values. var values = "My Name is #yourName# and I got #marks# in JavaScript subject"; We need to replace the special character placeholders with valid values. For this, we use replace() along with shift(). Using replace() with shift() The replace() method with a regular expression can find all instances of text surrounded by hash characters. The shift() method removes and returns the first element from an array, making it ...

Read More

What is the "get" keyword before a function in a class - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 510 Views

The get keyword in JavaScript creates a getter method that allows you to access a function like a property. When you call the getter, it executes the function and returns its value without using parentheses. Syntax class ClassName { get propertyName() { // return some value } } Basic Example Here's how to define and use a getter method: class Employee { constructor(name) { ...

Read More

Get the correct century from 2-digit year date value - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 383 Views

When working with 2-digit year values, you need to determine which century they belong to. A common approach is using a pivot year to decide between 19XX and 20XX centuries. Example Following is the code − const yearRangeValue = 18; const getCorrectCentury = dateValues => { var [date, month, year] = dateValues.split("-"); var originalYear = +year > yearRangeValue ? "19" + year : "20" + year; return new Date(originalYear + "-" + month + "-" + date).toLocaleDateString('en-GB') }; console.log(getCorrectCentury('10-JAN-19')); console.log(getCorrectCentury('10-JAN-17')); console.log(getCorrectCentury('10-JAN-25')); ...

Read More

How to merge specific elements inside an array together - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 171 Views

When working with arrays containing mixed data types, you might need to merge consecutive numeric elements while keeping certain separators intact. This is common when processing data that represents grouped numbers. Let's say we have the following array: var values = [7, 5, 3, 8, 9, '/', 9, 5, 8, 2, '/', 3, 4, 8]; console.log("Original array:", values); Original array: [7, 5, 3, 8, 9, '/', 9, 5, 8, 2, '/', 3, 4, 8] Using join() and split() Method To merge specific elements while preserving separators, we can use join(), split(), ...

Read More

Wrap object properties of type string with arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 593 Views

When working with objects, you may need to ensure all properties are arrays. This is useful for normalizing data structures where some properties might be strings while others are already arrays. The Problem Consider an object where some properties are strings and others are arrays. To process them uniformly, you need all properties to be arrays: var details = { name: ["John", "David"], age1: "21", age2: "23" }; console.log("Original object:"); console.log(details); Original object: { name: [ ...

Read More

Array filtering using first string letter in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Suppose we have an array that contains names of some people like this: const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia']; console.log(arr); [ 'Amy', 'Dolly', 'Jason', 'Madison', 'Patricia' ] We are required to write a JavaScript function that takes in one such array as the first argument, and two lowercase alphabet characters as second and third arguments. Then, our function should filter the array to contain only those elements that start with alphabets that fall within the range specified by the second and third arguments. Therefore, if the second and third arguments ...

Read More

Number of vowels within an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 799 Views

We are required to write a JavaScript function that takes in an array of strings, (they may be a single character or greater than that). Our function should simply count all the vowels contained in the array. Example Let us write the code − const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia']; const countVowels = (arr = []) => { const legend = 'aeiou'; const isVowel = c => legend.includes(c.toLowerCase()); let count = 0; arr.forEach(el => { for(let ...

Read More
Showing 1711–1720 of 8,010 articles
« Prev 1 170 171 172 173 174 801 Next »
Advertisements