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 341 of 840
How to add an element to a JSON object using JavaScript?
In this article, you will understand how to add an element to a JSON object using JavaScript. JSON objects are key-value pairs separated by colons and surrounded by curly braces {}. JavaScript provides two main ways to add new properties: bracket notation and dot notation. Using Bracket Notation Bracket notation is useful when property names contain special characters or are stored in variables. var jsonObject = { members: { host: "hostName", viewers: ...
Read MoreArray.prototype.fill() with object passes reference and not new instance in JavaScript?
When using Array.prototype.fill() with objects, JavaScript passes the same reference to all array positions instead of creating new instances. This means modifying one object affects all elements. The Problem with fill() and Objects Using fill() with an object creates multiple references to the same object: // This creates the same object reference in all positions let arr = new Array(3).fill({name: "John", age: 25}); console.log("Original array:"); console.log(arr); // Modifying one element affects all arr[0].name = "Alice"; console.log("After modifying arr[0].name:"); console.log(arr); Original array: [ { name: 'John', age: 25 }, { name: ...
Read MoreSum JavaScript arrays repeated value
Suppose, we have an array of objects like this − const arr = [ {'TR-01':1}, {'TR-02':3}, {'TR-01':3}, {'TR-02':5}]; We are required to write a JavaScript function that takes in one such array and sums the value of all identical keys together. Therefore, the summed array should look like − const output = [ {'TR-01':4}, {'TR-02':8}]; Method 1: Using In-Place Modification This approach modifies the original array by tracking duplicate keys and summing their values: const arr = [ {'TR-01':1}, {'TR-02':3}, {'TR-01':3}, {'TR-02':5}]; const sumDuplicate = arr => ...
Read MorePicking all elements whose value is equal to index in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should then construct and return a new array based on the original array. The new array should contain all those elements from the original array whose value was equal to the index they were placed on. Note that we have to check the value and index using 1-based index and not the traditional 0-based index. Understanding the Problem For example, if the input array is: const arr = [45, ...
Read MoreMathematics summation function in JavaScript
Problem We are required to write a JavaScript function that takes in a number n. Our function should return the sum of all the natural numbers from 1 to n including both 1 and n. Example Following is the code − const num = 34; const summation = (num = 1) => { let res = 0; for(let i = 1; i { return (num * (num + 1)) / 2; }; console.log(summationFormula(34)); // Same result console.log(summationFormula(100)); // Testing with larger number ...
Read MoreFinding average in mixed data type array in JavaScript
Suppose, we have an array of mixed data types like this − const arr = [1, 2, 3, 4, 5, "4", "12", "2", 6, 7, "4", 3, "2"]; We are required to write a JavaScript function that takes in one such array and returns the average of all such elements that are a number or can be partially or fully converted to a number. The string "3454fdf", isn't included in the problem array, but if it wasn't there, we would have used the number 3454 as its contribution to average. Example The code ...
Read MoreChecking for permutation of a palindrome in JavaScript
We are required to write a JavaScript function that takes in a string as the first and the only argument. The task of our function is to check whether any rearrangement in the characters of the string results into a palindrome string or not. If yes, then our function should return true, false otherwise. For a string to form a palindrome through rearrangement, at most one character can have an odd frequency. This is because palindromes read the same forwards and backwards. For example − If the input string is − const str = 'amadm'; ...
Read MoreValidating string with reference to array of words using JavaScript
We need to write a JavaScript function that takes an array of valid words and a string, then checks if the string can be formed by concatenating one or more words from the array. Problem Statement Given an array of words and a target string, determine if the string can be constructed using words from the array. Words can be reused multiple times. Input: const arr = ['love', 'coding', 'i']; const str = 'ilovecoding'; Expected Output: true The string "ilovecoding" can be formed by concatenating "i" + "love" + "coding". ...
Read MoreHow to calculate minutes between two dates in JavaScript?
In this article, you will understand how to calculate minutes between two dates in JavaScript. The Date object works with dates and times. Date objects are created with new Date(). To calculate minutes between dates, we convert the time difference from milliseconds to minutes using mathematical operations. Method 1: Using a Function In this example, we create a reusable function to find the time difference in minutes. function minutesDiff(dateTimeValue2, dateTimeValue1) { var differenceValue = (dateTimeValue2.getTime() - dateTimeValue1.getTime()) / 1000; differenceValue /= 60; return Math.abs(Math.round(differenceValue)); } ...
Read MoreCompare arrays using Array.prototype.every() in JavaScript
We are required to write a JavaScript function that takes in two arrays of literals. Then our function should return true if all the elements of first array are included in the second array, irrespective of their count, false otherwise. We have to use Array.prototype.every() method to make these comparisons. Syntax array.every(callback(element, index, array), thisArg) How Array.every() Works The every() { const areEqual = arr1.every(el => { return arr2.includes(el); }); return areEqual; }; ...
Read More