AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 322 of 840

Sorting an array of objects by an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 702 Views

In JavaScript, you can sort an array of objects based on the order defined by another array. This is useful when you need to prioritize objects according to a specific sequence. Problem Statement Given an array of objects and a reference array, we need to sort the objects according to the order specified in the reference array. const orders = [ { status: "pending"}, { status: "received" }, { status: "sent" }, { status: "pending" } ]; const statuses = ...

Read More

Converting object to 2-D array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 889 Views

In JavaScript, we often need to convert objects into 2D arrays where each sub-array contains a key-value pair. This is useful for data processing, iteration, or when working with APIs that expect array formats. Suppose we have an object that contains information about the weather of a city: const obj = { city: "New Delhi", maxTemp: 32, minTemp: 21, humidity: 78, aqi: 456, day: 'Tuesday', }; We need to convert this object into a 2D array where ...

Read More

Finding the immediate next character to a letter in string using JavaScript

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

We are required to write a JavaScript function that takes in a string of characters, str, and a single character, char. Our function should construct a new string that contains the immediate next character present in str after each instance of char (if any). Problem Given a string and a target letter, we need to find all characters that immediately follow each occurrence of the target letter and combine them into a new string. Solution We'll iterate through the string and check each character. When we find a match with our target letter, we'll add ...

Read More

Grouping and sorting 2-D array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 922 Views

Suppose we have a two-dimensional array of numbers like this − const arr = [ [1, 3, 2], [5, 2, 1, 4], [2, 1] ]; We are required to write a JavaScript function that groups all the identical numbers into their own separate subarray, and then the function should sort the group array to place the subarrays into increasing order. Therefore, finally the new array should look like − const output = [ [1, 1, 1], [2, 2, ...

Read More

Get range of months from array based on another array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 469 Views

Suppose we have two arrays of strings. The first array contains exactly 12 strings, one for each month of the year like this: const year = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; console.log(year); [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ] The second array contains exactly two strings, denoting a range of months like this: const monthsRange = ["aug", "oct"]; console.log(monthsRange); [ 'aug', 'oct' ] We need to ...

Read More

Converting array of arrays into an object in JavaScript

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

Converting an array of arrays into an object is a common task in JavaScript when dealing with key-value pair data. This is particularly useful when transforming data from APIs or CSV-like structures. Suppose we have an array of arrays that contains the performance of a cricket player like this: const arr = [ ['Name', 'V Kohli'], ['Matches', 13], ['Runs', 590], ['Highest', 183], ['NO', 3], ['SR', 131.5] ]; We need to write a JavaScript function that takes such ...

Read More

Finding all possible prime pairs that sum upto input number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 615 Views

Problem We need to write a JavaScript function that takes a number n and returns an array of all prime number pairs that sum to n. Both numbers in each pair must be prime numbers. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example: 2, 3, 5, 7, 11, 13, 17, 19, 23. Algorithm Approach The solution involves three main steps: Create a function to check if a number is prime Generate all possible prime numbers up to ...

Read More

How to calculate and print bonus and gross using basic salary by JavaScript?

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

We will first determine the bonus percentage by multiplying the basic salary by a certain percentage. Next, we will calculate the bonus amount by multiplying the bonus percentage with the basic salary. Finally, we will add the bonus amount to the basic salary to determine the gross salary and print all three values. Here is an example of how to calculate and print the bonus and gross salary using the basic salary in JavaScript − Basic Example // Declare basic salary variable var basicSalary = 5000; // Calculate bonus (10% of basic salary) var bonus ...

Read More

How to sort array according to age in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 472 Views

We are required to write a JavaScript function that takes in an array of numbers representing ages of some people. Then the function should bring all the ages less than 18 to the front of the array without using any extra memory. Understanding the Problem We need to sort an array so that all minors (ages < 18) appear before adults (ages ≥ 18), while maintaining the original order within each group. Example The code for this will be − const ages = [23, 56, 56, 3, 67, 8, 4, 34, 23, 12, ...

Read More

Finding the index position of an array inside an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 287 Views

When working with arrays of arrays in JavaScript, you might need to find the index position of a specific sub-array within the main array. This is useful for checking if a particular array exists and determining its location. Suppose we have an array of arrays like this: const arr = [ [1, 0], [0, 1], [0, 0] ]; We need to write a JavaScript function that takes an array of arrays as the first argument and a target array as the second argument. The function should ...

Read More
Showing 3211–3220 of 8,392 articles
« Prev 1 320 321 322 323 324 840 Next »
Advertisements