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
Object Oriented Programming Articles
Page 142 of 589
Split a URL in JavaScript after every forward slash?
To split a URL in JavaScript, use the split() method with a forward slash (/) as the delimiter. This breaks the URL into an array of segments. Syntax url.split("/") Basic Example var newURL = "http://www.example.com/index.html/homePage/aboutus/"; console.log("Original URL:", newURL); var splitURL = newURL.split("/"); console.log("Split URL:", splitURL); Original URL: http://www.example.com/index.html/homePage/aboutus/ Split URL: [ 'http:', '', 'www.example.com', 'index.html', 'homePage', 'aboutus', '' ] Understanding the Output The result contains empty strings because: Index 0: 'http:' ...
Read MoreGroup values in array by two properties JavaScript
We have an array of objects like this: const arr = [ { value: 12, gap: 1 }, { value: 13, gap: 1 }, { value: 14, gap: 1 }, { value: 15, gap: 1 }, { value: 19, gap: 2 }, { value: 21, gap: 1 }, { value: 22, gap: 1 }, { value: 23, gap: 1 }, { value: ...
Read MoreHow to count the number of elements in an array below/above a given number (JavaScript)
In JavaScript, we often need to analyze arrays and count elements based on certain conditions. This article shows how to count array elements that fall below or above a given threshold number. Consider we have an array of numbers like this: const array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9]; We need to write a function that counts how many elements are below and above a given number. For example, if the number is 5.25, there should be 5 elements below it: (3.1, 1, 2.2, 5.1, 2.1) And ...
Read MoreReplace array value from a specific position in JavaScript
To replace a value at a specific position in a JavaScript array, you can use the splice() method or direct index assignment. Both approaches modify the original array. Using splice() Method The splice() method removes elements and optionally adds new ones at a specified position. Syntax array.splice(index, deleteCount, newElement) Example var changePosition = 2; var listOfNames = ['John', 'David', 'Mike', 'Sam', 'Carol']; console.log("Before replacing:"); console.log(listOfNames); var name = 'Adam'; var result = listOfNames.splice(changePosition, 1, name); console.log("After replacing:"); console.log(listOfNames); console.log("Removed element:", result); Before replacing: [ 'John', ...
Read MoreRearrange an array in maximum minimum form by JavaScript
We are required to write a function, say minMax() that takes in an array of Numbers and rearranges the elements such that the greatest element appears first followed by the smallest elements then the second greatest element followed by second smallest element and so on. For example − // if the input array is: const input = [1, 2, 3, 4, 5, 6, 7] // then the output should be: const output = [7, 1, 6, 2, 5, 3, 4] So, let's write the complete code for this function − Approach: Using Two Pointers ...
Read MoreHow to get sequence number in loops with JavaScript?
To get sequence numbers in loops with JavaScript, you can use various approaches. The most common method is maintaining a counter variable that increments with each iteration. Using forEach() with External Counter The forEach() method provides an elegant way to iterate through arrays while maintaining sequence numbers using an external counter: let studentDetails = [ { id: 101, details: [{name: 'John'}, {name: 'David'}, {name: 'Bob'}] }, { ...
Read MoreHow to check if every property on object is the same recursively in JavaScript?
When working with nested objects in JavaScript, you might need to check if all leaf values (final non-object values) are identical. This requires a recursive approach to traverse through all nested levels and compare the actual values at the end of each branch. For example, in this object: const obj = { a: 1, b: 1, c: { aa: 1 } }; The function should return true because all leaf values equal 1, even though one is ...
Read MoreSplit First name and Last name using JavaScript?
In JavaScript, you can split a full name into first and last name using the split() method. This method divides a string based on a specified separator and returns an array of substrings. Syntax string.split(separator) Basic Example var studentFullName = "John Smith"; var details = studentFullName.split(' '); console.log("Student First Name = " + details[0]); console.log("Student Last Name = " + details[1]); Student First Name = John Student Last Name = Smith Handling Multiple Names For names with more than two parts, you can extract the first ...
Read MoreParse array to equal intervals in JavaScript
Let's say, we are required to write a function, say parseEqualInterval() that takes in an array of Numbers of strictly two elements as the first argument and a number n as the second argument and it inserts n-1 equidistant entries between the actual two elements of the original array so that it gets divided into n equal intervals. For example: // if the input array is const arr = [12, 48]; // and the interval is 4 //then the output array should be: const output = [12, 21, 30, 39, 48]; This way the array ...
Read MoreReplace commas with JavaScript Regex?
In JavaScript, you can replace commas in strings using the replace() method combined with regular expressions. This is particularly useful when you need to replace specific comma patterns, such as the last comma in a string. Problem Statement Consider these example strings with commas: "My Favorite subject is, " "My Favorite subject is, and teacher name is Adam Smith" "My Favorite subject is, and got the marks 89" We want to replace the last comma in each string with " JavaScript". Using Regular Expression to Replace Last Comma The regular expression /, ...
Read More