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
Articles by AmitDiwan
Page 430 of 840
How to convert a string to JavaScript object?
In JavaScript, you can convert a JSON string to an object using the JSON.parse() method. This is commonly needed when working with API responses or stored data. Syntax JSON.parse(string) Basic Example String to Object Conversion Original JSON String: {"name":"Rohan", "sports":["Cricket", "Football"], "country":"India"} Converted Object Properties: Convert to Object ...
Read MoreCreate a polyfill to replace nth occurrence of a string JavaScript
A polyfill is a piece of code that provides functionality that isn't natively supported. In this tutorial, we'll create a polyfill to remove the nth occurrence of a substring from a string in JavaScript. Problem Statement We need to create a polyfill function removeStr() that extends the String prototype. The function should: subStr → the substring whose nth occurrence needs to be removed num → the occurrence number to remove (1st, 2nd, 3rd, etc.) Return the modified string if successful, or -1 if the nth occurrence doesn't exist ...
Read MoreHow to remove certain number elements from an array in JavaScript
We are required to write a function that takes in an array of numbers and a number, and it should remove all the occurrences of that number from the array in-place. Let's explore different approaches to accomplish this task effectively. Method 1: Using Recursion with splice() We can use recursion to remove elements by finding and splicing them one by one. The recursive function continues until no more occurrences are found. const numbers = [1, 2, 0, 3, 0, 4, 0, 5]; const removeElement = (arr, element) => { if(arr.indexOf(element) !== ...
Read MoreHow to Hide a div in JavaScript on Button Click?
To hide a div in JavaScript on button click we will be discussing three different approaches with example codes. We will hide the div upon clicking the button and similarly display the hidden div upon clicking the button. In this article we are having a div element. Our task is to hide the div on clicking the button using JavaScript. Approaches to Hide div on Button Click Here is a list of approaches to hide a div in JavaScript on button click which we will be discussing in this article with stepwise explanation and complete example codes. ...
Read MoreChecking if two arrays can form a sequence - JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers. And the function should return true if the two arrays upon combining and shuffling can form a consecutive sequence, false otherwise. For example − If the arrays are − const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7]; Then the output should be true because when combined and sorted, they form: [1, 2, 3, 4, 5, 6, 7, 8, 9] which is a consecutive sequence. Example Following is the code − ...
Read MoreAdd oninput attribute to HTML element with JavaScript?
You can add the oninput attribute to HTML elements using JavaScript in two ways: using addEventListener() or directly setting the oninput property. Both methods allow you to dynamically attach input event handlers to elements. Using addEventListener() (Recommended) The addEventListener() method is the modern approach for attaching event listeners: Add oninput with addEventListener Enter the value: ...
Read MoreHow to convert array of comma separated strings to array of objects?
Converting an array of comma-separated strings to an array of objects is a common task in JavaScript. This can be done using various methods depending on the structure of your data. Understanding the Problem When you have an array containing JSON strings, you need to parse each string into a JavaScript object. The most straightforward approach is using JSON.parse() combined with array methods. Method 1: Using JSON.parse() with forEach This method modifies the original array by parsing each JSON string: ...
Read MoreGet the index of the nth item of a type in a JavaScript array
When working with JavaScript arrays, you might need to find the index of the nth occurrence of a specific value. This is useful for parsing data, finding patterns, or processing structured arrays. Problem Statement We need to write a function getIndex() that takes three parameters: an array arr, a value txt (string or number), and a number n. The function should return the index of the nth appearance of txt in arr. If txt doesn't appear n times, return -1. Using Array.reduce() Method The reduce() method provides an elegant solution by maintaining a counter and tracking ...
Read MoreHow to capitalize the first letter of each word in a string using JavaScript?
In JavaScript, you can capitalize the first letter of each word in a string by splitting the string into individual words, capitalizing each word's first letter, and then joining them back together. Syntax string.split(' ') // Split string into array of words word.charAt(0) // Get first character word.toUpperCase() // Convert to uppercase word.substring(1) // Get ...
Read MoreCount the number of data types in an array - JavaScript
We are required to write a JavaScript function that takes in an array that contains elements of different data types and the function should return a map representing the frequency of each data type. Let's say the following is our array: const arr = [23, 'df', undefined, null, 12, { name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8]; Understanding Data Types in JavaScript JavaScript's typeof operator returns string representations of data types. Note that arrays and null both return "object", which is a known quirk of JavaScript. Example ...
Read More