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 438 of 840
How to set JavaScript object values dynamically?
JavaScript objects allow dynamic property assignment using bracket notation or dot notation. This enables you to set object values at runtime based on variables or user input. Syntax // Using dot notation object.propertyName = value; // Using bracket notation (dynamic) object[propertyName] = value; object['property name'] = value; Method 1: Using Dot Notation Dot notation works when you know the property name at compile time: Dynamic Object Values body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...
Read MoreHow to use my object like an array using map function in JavaScript?
JavaScript objects aren't arrays, so they don't have array methods like map(). However, you can use Object.keys(), Object.values(), or Object.entries() to convert object data into arrays, then apply map(). Using Object.keys() with map() Extract object keys as an array, then use map() to iterate: const object = { name: 'John', age: 21, countryName: 'US', subjectName: 'JavaScript' }; const allKeys = Object.keys(object); console.log("All keys:", allKeys); // Using map to get values from keys const mappedValues = allKeys.map(key => object[key]); ...
Read MoreModifying prototypes in JavaScript
In JavaScript, prototypes allow you to add properties and methods to constructor functions. You can modify prototypes even after objects are created, and all existing instances will automatically inherit the changes. Understanding Prototypes Every function in JavaScript has a prototype property. When you create objects using a constructor function, they inherit methods and properties from the constructor's prototype. Example: Modifying Prototypes Modifying Prototypes body { ...
Read MoreCompute the sum of elements of an array which can be null or undefined JavaScript
When working with arrays containing numbers, null, and undefined values, we need to compute sums while treating these falsy values as zero. This is a common scenario when processing data from APIs or user inputs. Let's say we have an array of arrays, each containing some numbers along with some undefined and null values. We need to create a new array that contains the sum of each corresponding sub-array elements, treating undefined and null as 0. Following is the sample array: const arr = [[ 12, 56, undefined, 5 ], [ ...
Read MoreConditional statements in JavaScript
Conditional statements allow you to execute different blocks of code based on specific conditions. JavaScript provides three main types of conditional statements to control program flow. Types of Conditional Statements if statement − Executes code only if a specific condition is true. if...else statement − Checks one condition and executes different code blocks for true and false cases. if...else if...else statement − Handles multiple conditions by checking them sequentially. Syntax // if statement if (condition) { // code to execute ...
Read MoreJavaScript reduce sum array with undefined values
If you're working with arrays in JavaScript, you may encounter situations where your array contains undefined values. This can happen when you're processing data from various sources or working with incomplete datasets. One common problem developers face is summing the values of an array with undefined elements. JavaScript's reduce() method is a versatile tool that can help you solve this problem efficiently. What is the reduce() Method in JavaScript? The reduce() method in JavaScript is a powerful tool that allows you to iterate over an array and accumulate a single result. It takes a callback function as its ...
Read MoreReduce sum of digits recursively down to a one-digit number JavaScript
We have to write a function that takes in a number and keeps adding its digits until the result is a one-digit number. When we have a one-digit number, we return it. The code uses a recursive function that keeps adding digits until the number is between -9 and 9. We handle the sign separately to avoid duplicating logic. How It Works The algorithm follows these steps: Take the absolute value of the number to handle sign separately If the number is greater than 9, split it into digits and sum them Recursively call the ...
Read MoreChanging value of nested object keys in JavaScript
In JavaScript, you can change nested object values using dot notation and square brackets. This is useful when working with complex data structures containing multiple levels of nesting. Syntax // Using dot notation object.property.nestedProperty = newValue; // Using square brackets object['property']['nestedProperty'] = newValue; // Mixed approach object.property['nestedProperty'] = newValue; Example: Changing Nested Object Values var details = { "customer": { "customerDetails": { "otherDetails": [ ...
Read MoreIndexed collections in JavaScript
JavaScript provides indexed collections that allow you to store and access elements using numeric indices. Arrays are the most common indexed collection, where each element has a position (index) starting from 0. What are Indexed Collections? Indexed collections are data structures where elements are accessed using numerical indices. In JavaScript, arrays are the primary indexed collection type, allowing you to store multiple values and retrieve them using their position. Basic Array Creation and Access Indexed Collections ...
Read MoreRemove number properties from an object JavaScript
In JavaScript, we often need to filter object properties based on their data types. This article demonstrates how to remove properties of a specific type from an object using a reusable function. Problem Statement Given an object with mixed property types (numbers, strings, booleans, objects), we need to create a function that removes all properties of a specified data type. If no type is specified, it should default to removing number properties. Solution We'll create a function called shedData that iterates through object properties and deletes those matching the specified type: const obj = ...
Read More