Web Development Articles

Page 183 of 801

Fetch specific values from array of objects in JavaScript?

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

Fetching specific values from an array of objects is a common JavaScript task. This article demonstrates multiple approaches to filter and extract data based on specific criteria. Sample Data Let's start with an array of employee objects: const details = [ { employeeFirstName: "John", employeeLastName: "Doe" }, { employeeFirstName: "David", employeeLastName: "Miller" ...

Read More

Map anagrams to one another in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 279 Views

One array is an anagram of another if we can rearrange the elements of that array to achieve the other array. For example: [1, 2, 3] and [2, 1, 3] are anagrams of each other. We need to write a JavaScript function that takes two anagram arrays and returns a mapping array. The mapping array contains the index of each element from the first array as it appears in the second array. Problem Example If the two input arrays are: const arr1 = [23, 39, 57, 43, 61]; const arr2 = ...

Read More

Enter values with prompt and evaluate on the basis of conditions in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 299 Views

JavaScript's prompt() function allows you to collect user input and evaluate it using conditional statements. This is useful for creating interactive web applications that respond based on user-provided values. Basic Syntax To get user input and convert it to a number: var value = parseInt(prompt("Enter a value")); Example: Conditional Evaluation Based on User Input Here's a complete example that prompts for two values and evaluates their product: Prompt and Evaluate ...

Read More

Converting whitespace string to url in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 720 Views

In web URLs, browsers automatically replace spaces with '%20' for proper encoding. JavaScript provides multiple methods to convert whitespace characters to URL-encoded format. We need to write a function that takes a string and replaces all whitespace characters with '%20'. For example, if the input string is: const str = 'some extra Space'; Then the output should be: const output = 'some%20extra%20%20Space'; Using Manual Loop Method This approach iterates through each character and manually replaces spaces: const str = 'some extra Space'; const replaceWhitespace = (str = ...

Read More

Remove/ filter duplicate records from array - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 314 Views

JavaScript arrays often contain duplicate records that need to be removed. There are several effective methods to filter duplicates, depending on whether you're working with primitive values or objects. Sample Data with Duplicates Let's start with an array of nationality objects containing duplicate values: var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ]; console.log("Original array:", objectOfNationality); ...

Read More

Finding elements whose successors and predecessors are in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 518 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function should construct and return a new array that contains all such elements from the original array whose successor and predecessor are both present in the array. This means, if any element num is in the original array, it should be included in the result array if and only if num - 1 and num + 1 are also present in the array. For example, if the input array is: const arr = ...

Read More

Number prime test in JavaScript by creating a custom function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In JavaScript, we can create a custom function to check if a number is prime. Basic Prime Number Function Here's a simple approach to check if a number is prime: function checkNumberIsPrime(number) { if (number

Read More

Finding confusing number within an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 318 Views

A number in an array is confusing if it becomes another number which is also present in the array after we rotate it by 180 degrees. When rotated, digits transform as follows: 0→0, 1→1, 6→9, 8→8, 9→6. Other digits (2, 3, 4, 5, 7) cannot be rotated to form valid numbers. Understanding Confusing Numbers For a number to be confusing: It must contain only the digits 0, 1, 6, 8, 9 When rotated 180 degrees, it must form a valid number within our range The rotated number must also be present in the array Example ...

Read More

Comparing adjacent element and swap - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 471 Views

Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. This process repeats until the array is sorted. How Bubble Sort Works The algorithm repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. Each pass "bubbles" the largest element to its correct position. Bubble Sort Process Initial: 10 30 5 ...

Read More

Checking a Checkbox with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 618 Views

In JavaScript, you can programmatically check a checkbox by setting its checked property to true. This is useful for form validation, user interactions, or initializing form states. HTML Structure First, let's look at a basic checkbox structure: John David Using the checked Property The checked property is a boolean that determines whether a checkbox is selected. Set it to true to check the checkbox, or false to uncheck it. Checkbox Example ...

Read More
Showing 1821–1830 of 8,010 articles
« Prev 1 181 182 183 184 185 801 Next »
Advertisements