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 73 of 534
How to generate array key using array index – JavaScript associative array?
In JavaScript, you can create associative arrays (objects) using array indices as keys. This is useful for mapping array elements to their positions or creating key-value pairs from existing arrays. Using forEach() Method The forEach() method provides access to both the element and its index, allowing you to create dynamic object properties: var result = {}; var names = ['John', 'David', 'Mike', 'Sam', 'Bob', 'Adam']; names.forEach((nameObject, counter) => { var generatedValues = { [nameObject]: counter }; Object.assign(result, generatedValues); }); console.log(result); { John: 0, ...
Read MoreAdd time to string data/time - JavaScript?
In JavaScript, you can add time to a date/time string using the Date object's built-in methods. This is useful for calculating future times or adjusting existing timestamps. Basic Approach First, create a Date object from your string, then use methods like setHours(), setMinutes(), or setSeconds() combined with their getter counterparts to add time. var dateValue = new Date("2021-01-12 10:10:20"); dateValue.setHours(dateValue.getHours() + 2); // Add 2 hours Example: Adding Hours to Date String var dateValue = new Date("2021-01-12 10:10:20"); console.log("Original date: " + dateValue.toString()); // Add 2 hours dateValue.setHours(dateValue.getHours() + 2); ...
Read MoreHow to sort array by first item in subarray - JavaScript?
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 MoreHow to decrease size of a string by using preceding numbers - JavaScript?
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 MoreES6 Default Parameters in nested objects – JavaScript
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 MoreReplace multiple instances of text surrounded by specific characters in JavaScript?
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 MoreWhat is the "get" keyword before a function in a class - JavaScript?
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 MoreGet the correct century from 2-digit year date value - JavaScript?
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 MoreHow to merge specific elements inside an array together - JavaScript
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 MoreWrap object properties of type string with arrays - JavaScript
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