Object Oriented Programming Articles

Page 35 of 589

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 169 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 168 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 826 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 211 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 316 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

How to make Ulam number sequence in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 189 Views

A mathematician Ulam proposed generating a sequence of numbers from any positive integer n (n>0) as follows: If n is 1, it will stop. if n is even, the next number is n/2. if n is odd, the next number is 3 * n + 1. continue with the process until reaching 1. This sequence is also known as the Collatz conjecture or 3n+1 problem. It's conjectured that all positive integers eventually reach 1 following these rules. Example Sequences Here are some examples for the first few integers: 2→1 3→10→5→16→8→4→2→1 4→2→1 6→3→10→5→16→8→4→2→1 ...

Read More

Finding the smallest value in a JSON object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 905 Views

Finding the smallest value in a JSON object is a common task in JavaScript. This involves traversing through an object's properties and comparing their numeric values to determine the minimum. When working with objects that contain numeric values, we need to iterate through all properties and find the one with the smallest value. JavaScript provides several approaches to accomplish this efficiently. Example Here's how to find the smallest value using the reduce() method: const obj = { "a": 4, "b": 2, "c": 5, ...

Read More
Showing 341–350 of 5,881 articles
« Prev 1 33 34 35 36 37 589 Next »
Advertisements