Javascript Articles

Page 116 of 534

How to automate this object using JavaScript to set one key on each iteration as null?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

When working with JavaScript objects, you might need to systematically set each property to null one at a time. This can be useful for testing, validation, or creating variations of an object. The most efficient approach is using Object.keys() with the spread operator. Understanding the Problem Given an object with multiple properties, we want to create new objects where exactly one property is set to null in each iteration, while keeping all other properties unchanged. Using Object.keys() with Spread Operator The Object.keys() method returns an array of property names, which we can iterate through. The spread ...

Read More

How to get id from tr tag and display it in a new td with JavaScript?

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

When working with HTML tables, you often need to extract the ID from table rows and display them within the table itself. This tutorial shows how to get the ID from tags and add them as new table cells using JavaScript. Sample Table Structure Consider the following table with ID attributes on each row: StudentName StudentCountryName ...

Read More

What's the most efficient way to turn all the keys of an object to lower case - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 232 Views

When working with JavaScript objects, you may need to convert all property keys to lowercase for consistency. Here's how to efficiently transform object keys using built-in JavaScript methods. Sample Object Let's start with an object that has uppercase keys: var details = { "STUDENTNAME": "John", "STUDENTAGE": 21, "STUDENTCOUNTRYNAME": "US" }; console.log("Original object:", details); Original object: { STUDENTNAME: 'John', STUDENTAGE: 21, STUDENTCOUNTRYNAME: 'US' } Method 1: Using Object.keys() and while Loop This approach uses a while loop to iterate ...

Read More

Make JavaScript take HTML input from user, parse and display?

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

JavaScript can retrieve HTML input values as strings and parse them into different data types. This tutorial shows how to get user input from HTML forms and convert it appropriately. Basic Input Retrieval HTML input elements return string values by default. Use document.getElementById() to access input values: Input Parser Example Parse Input function parseInput() { ...

Read More

Algorithm to sum ranges that lie within another separate range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

We have two sets of ranges; one is a single range of any length (R1) and the other is a set of ranges (R2) some or parts of which may or may not lie within the single range (R1). We need to calculate the sum of the ranges in (R2) - whole or partial - that lie within the single range (R1). Problem Breakdown For each range in R2, we calculate the overlapping portion with R1 and sum all overlaps: const R1 = [20, 40]; const R2 = [[14, 22], [24, 27], [31, 35], [38, ...

Read More

How to iterate an array of objects and build a new one in JavaScript ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 166 Views

Suppose, we have an array of objects like this: const arr = [ { "customer": "Customer 1", "project": "1" }, { "customer": "Customer 2", "project": "2" }, { "customer": "Customer 2", ...

Read More

How to convert square bracket object keys into nested object in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 823 Views

In JavaScript, objects with square bracket notation in their keys can be converted into properly nested objects. This is useful when dealing with flat data structures that represent nested relationships. Consider an object with square bracket notation: const flatObj = { "object[foo][bar][ya]": 100 }; console.log("Original object:", flatObj); Original object: { 'object[foo][bar][ya]': 100 } We want to convert this into a nested structure where each bracket represents a deeper level of nesting. Understanding the Problem The goal is to transform a key like "object[foo][bar][ya]" into a nested object structure: ...

Read More

Filtering array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 849 Views

Filtering arrays of objects is a common task in JavaScript. This article shows how to filter an array of objects based on values from another array using the filter() and includes() methods. Problem Statement Suppose we have two arrays - one containing literal values and another containing objects: const source = [1, 2, 3, 4, 5]; const cities = [{ city: 4 }, { city: 6 }, { city: 8 }]; console.log("Source array:", source); console.log("Cities array:", cities); Source array: [1, 2, 3, 4, 5] Cities array: [{ city: 4 }, { city: ...

Read More

Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

This tutorial shows how to find pairs of numbers in an array that sum to a target value, then return the sum of all indices of these unique pairs. Problem Description Given an array of numbers and a target sum, we need to: Find all pairs of numbers that sum to the target Ensure each number is used only once across all pairs Return the sum of indices of all numbers used in valid pairs Example Walkthrough For array [1, 4, 2, 3, 0, 5] with target sum 7: Valid pairs: 4 ...

Read More

Read by key and parse as JSON in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 310 Views

In JavaScript, you often need to group JSON data by specific keys and transform the structure. This tutorial shows how to read data by key values and reorganize it into a grouped JSON format. Problem Statement Given a JSON array with nested data, we want to group items by a specific key (like "W") and create a new structure where each group becomes a separate object. Starting with this data structure: const arr = [{ "data": [ { "W": 1, "A1": "123" }, ...

Read More
Showing 1151–1160 of 5,340 articles
« Prev 1 114 115 116 117 118 534 Next »
Advertisements