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 393 of 840
How to remove li elements on button click in JavaScript?
In JavaScript, you can remove list items dynamically by attaching event listeners to buttons within each element. This tutorial shows how to remove specific list items when their corresponding "Remove" buttons are clicked. HTML Structure First, let's look at the basic HTML structure for our unordered list: JavaScript Remove MySQL Remove MongoDB Remove Java Remove Each list item contains a subject name and a "Remove" button. When clicked, the button will remove its parent ...
Read MorePrint JSON nested object in JavaScript?
To print JSON nested objects in JavaScript, you can use various approaches depending on the structure of your data. When dealing with nested JSON strings within objects, you'll need to parse them first using JSON.parse(). Example: Parsing Nested JSON Strings Here's how to handle objects containing JSON strings as properties: var details = [ { "studentId": 101, "studentName": "John", "countryName": "US", ...
Read MoreAccessing variables in a constructor function using a prototype method with JavaScript?
In JavaScript constructor functions, you can access instance variables from prototype methods using the this keyword. Prototype methods share behavior across all instances while maintaining access to individual instance data. How Constructor Functions Work When you create a constructor function, instance variables are defined using this.propertyName. Prototype methods can then access these variables through this. Example function Customer(fullName){ this.fullName = fullName; } Customer.prototype.setFullName = function(newFullName){ this.fullName = newFullName; } Customer.prototype.getFullName = function(){ return this.fullName; } var customer = new Customer("John ...
Read MoreRetrieving object's entries in order with JavaScript?
In JavaScript, object properties don't maintain insertion order for numeric keys, but we can retrieve entries in sorted order using Object.keys() and sort() methods. The Problem Consider an object with numeric keys that aren't in sequential order: const subjectDetails = { 102: "Java", 105: "JavaScript", 104: "MongoDB", 101: "MySQL" }; console.log("Original object:"); console.log(subjectDetails); Original object: { '101': 'MySQL', '102': 'Java', '104': 'MongoDB', '105': 'JavaScript' } ...
Read MoreSplitting an array based on its first value - JavaScript
Suppose we have an array of arrays of numbers like this: const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]]; console.log(arr); [ [ 1, 45 ], [ 1, 34 ], [ 1, 49 ], [ 2, 34 ], [ 4, 78 ], [ 2, 67 ], [ 4, 65 ] ] Each subarray contains exactly two elements. We need to write a function that constructs a new array where all second elements of subarrays with the same first value are grouped together. For the ...
Read MoreFlat a JavaScript array of objects into an object
To flatten a JavaScript array of objects into a single object, we can create a function that iterates through each object in the array and combines their properties. This technique is useful when you need to merge multiple objects while preserving unique property names by appending indices. Basic Approach The most straightforward method is to loop through the array and create new property names by appending the array index to each original property name. // Example array of objects const notes = [{ title: 'Hello world', id: 1 ...
Read MoreFilter null from an array in JavaScript?
To filter null values from an array in JavaScript, use the filter() method. This method creates a new array containing only elements that pass a specified condition. Syntax array.filter(callback) Method 1: Using Boolean Constructor The simplest approach is to pass Boolean as the filter callback, which removes all falsy values including null, undefined, and empty strings. var names = [null, "John", null, "David", "", "Mike", null, undefined, "Bob", "Adam", null, null]; console.log("Before filtering:"); console.log(names); var filteredNames = names.filter(Boolean); console.log("After filtering null/falsy values:"); console.log(filteredNames); Before filtering: [ ...
Read MoreConvert number to tens, hundreds, thousands and so on - JavaScript
We are required to write a function that, given a number, say, 123, will output an array − [100, 20, 3] Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function. We can solve this problem by using a recursive approach. Example Following is the code − const num = 123; const placeValue = (num, res = [], factor = 1) => { if(num){ ...
Read MoreIs it possible to have JavaScript split() start at index 1?
The built-in String.prototype.split() method doesn't have a parameter to start splitting from a specific index. However, we can combine split() with array methods like slice() to achieve this functionality. Problem with Standard split() The standard split() method always starts from index 0 and splits the entire string: const text = "The quick brown fox jumped over the wall"; console.log(text.split(" ")); [ 'The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'wall' ] Method 1: Using split() with slice() The simplest ...
Read MoreDisplay a message on console while focusing on input type in JavaScript?
In JavaScript, you can display console messages when focusing on input elements using the focus and blur event listeners. This is useful for debugging form interactions and tracking user behavior. Basic Focus Event Example The focus event triggers when an input element receives focus, while blur triggers when it loses focus: Focus Console Messages Submit ...
Read More