Web Development Articles

Page 464 of 801

Sum from array values with similar key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 603 Views

When working with arrays of data, you often need to group and sum values based on a common key. This article demonstrates how to sum array values with similar keys using JavaScript's reduce() method. Let's say we have an array containing stock transaction data for a company over time: const transactions = [ ['AAPL', 'buy', 100], ['WMT', 'sell', 75], ['MCD', 'buy', 125], ['GOOG', 'sell', 10], ['AAPL', 'buy', 100], ['AAPL', 'sell', 100], ...

Read More

Finding roots of a quadratic equation – JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 602 Views

A quadratic equation has the form ax² + bx + c = 0, where a, b, and c are coefficients. To find the roots, we use the quadratic formula and check if the discriminant is non-negative for real roots. Quadratic Formula The quadratic formula is: x = (-b ± √(b² - 4ac)) / (2a) The discriminant (b² - 4ac) determines the nature of roots: If discriminant > 0: Two distinct real roots If discriminant = 0: One repeated real root If discriminant < 0: No real roots (complex roots) Example ...

Read More

How to splice duplicate item in array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 718 Views

We have an array of Number / String literals that contain some duplicate values, we have to remove these values from the array without creating a new array or storing the duplicate values anywhere else. We will use the Array.prototype.splice() method to remove entries in-place, and we will take help of Array.prototype.indexOf() and Array.prototype.lastIndexOf() method to determine the duplicacy of any element. The Problem with Forward Iteration When using forEach() to remove duplicates, we encounter index shifting issues. Here's why the basic approach doesn't work perfectly: const arr = [1, 4, 6, 1, 2, 5, ...

Read More

Nearest power 2 of a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 910 Views

We need to write a JavaScript function that takes a number and returns the nearest power of 2. A power of 2 is any number that can be expressed as 2n where n is a whole number (1, 2, 4, 8, 16, 32, 64, 128, 256, etc.). For example, if the input is 365, the output should be 256 because 256 (28) is closer to 365 than the next power of 2, which is 512 (29). Algorithm Explanation The algorithm works by: Starting with base = 1 (20) Doubling the base until it exceeds the input ...

Read More

How to combine 2 arrays into 1 object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 818 Views

Let's say, we have two arrays of equal lengths and are required to write a function that maps the two arrays into an object. The corresponding elements of the first array becomes the corresponding keys of the object and the elements of the second array become the value. We will reduce the first array, at the same time accessing elements of the second array by index. This is a common pattern for creating objects from parallel arrays. Using Array.reduce() Method const keys = [ 'firstName', 'lastName', 'isEmployed', ...

Read More

Finding area of triangle in JavaScript using Heron's formula

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 964 Views

We are given the lengths of three sides of a triangle and we are required to write a function that returns the area of the triangle using the length of its sides. Heron's Formula We can calculate the area of a triangle if we know the lengths of all three sides, using Heron's formula: Step 1 − Calculate "s" (half of the triangle's perimeter): s = (a + b + c) / 2 Step 2 − Then calculate the Area using Heron's formula: A = √(s(s-a)(s-b)(s-c)) Syntax ...

Read More

How to Sort object of objects by its key value JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 695 Views

Let's say, we have an object with keys as string literals and their values as objects as well like this − const companies = { 'landwaves ltd': {employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal'}, 'colin & co': {employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari'}, 'motilal biscuits': {employees: 975, worth: '1m', CEO: 'Rahul Gupta'}, 'numbtree': {employees: 1500, worth: '1.5m', CEO: 'Jay Kumar'}, 'solace pvt ltd': {employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal'}, 'ambicure': {employees: 170, worth: '0.1m', CEO: 'Preetam Chawla'}, ...

Read More

Order an array of words based on another array of words JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 429 Views

When working with arrays in JavaScript, you may need to reorder an array of objects based on the order specified in another array. This is useful for sorting data according to custom priorities or sequences. Let's say we have an array of objects sorted by their id property: const unordered = [{ id: 1, string: 'sometimes' }, { id: 2, string: 'be' }, { id: 3, string: 'can' }, { ...

Read More

Split string into groups - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

Given a string that consists of alphabets, numbers, and special characters, we need to split it into three separate strings based on character type. We'll create three strings where: S1 contains all alphabets from the original string S2 contains all numbers from the original string S3 contains all special characters from the original string The characters in each resulting string maintain the same order as they appear in the input string. Example Implementation const str = "Th!s String C0nt@1ns d1fferent ch@ract5rs"; const ...

Read More

Merge objects in array with similar key JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

When working with arrays of objects in JavaScript, you might need to merge objects that share a common key. This is useful when consolidating data from multiple sources or restructuring existing data. Let's say we have the following array of objects where each object has an id and various heading properties: const arr = [ {id: 1, h1: 'Daily tests'}, {id: 2, h1: 'Details'}, {id: 1, h2: 'Daily classes'}, {id: 3, h2: 'Results'}, {id: 2, h3: 'Admissions'}, ...

Read More
Showing 4631–4640 of 8,010 articles
« Prev 1 462 463 464 465 466 801 Next »
Advertisements