Javascript Articles

Page 455 of 534

How to move multiple elements to the beginning of the array in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 795 Views

In JavaScript, you can move multiple elements to the beginning of an array by identifying their positions and using array methods like splice() and unshift(). This technique is useful when you need to prioritize certain elements while maintaining the relative order of others. Problem Overview We need to create a function that takes an array and any number of strings as arguments. The function should check if these strings exist in the array and move them to the front while preserving their relative order. Using splice() and unshift() Method This approach removes elements from their current ...

Read More

JavaScript Match between 2 arrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 387 Views

When working with two arrays in JavaScript, you often need to match elements from one array with corresponding elements in another. This article demonstrates how to extract user IDs from an object array based on a names array, maintaining the order of the names array. Problem Statement We have two arrays: one containing user objects with names and UIDs, and another containing just names. Our goal is to create a function that returns UIDs in the same order as they appear in the names array. const data = [{ name: 'Kamlesh Kapasi', ...

Read More

Sort array of points by ascending distance from a given point JavaScript

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

Let's say, we have an array of objects with each object having exactly two properties, x and y that represent the coordinates of a point. We have to write a function that takes in this array and an object with x and y coordinates of a point and we have to sort the points (objects) in the array according to the distance from the given point (nearest to farthest). The Distance Formula It is a mathematical formula that states that the shortest distance between two points (x1, y1) and (x2, y2) in a two-dimensional plane is given by ...

Read More

How to calculate the average in JavaScript of the given properties in the array of objects

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

We have an array of objects. Each object contains a few properties and one of these properties is age: const people = [ { name: 'Anna', age: 22 }, { name: 'Tom', age: 34 }, { name: 'John', ...

Read More

How to get odd and even position characters from a string?

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

In JavaScript, you can extract characters from odd and even positions in a string using various methods. This technique is useful for string manipulation tasks like creating puzzles, encoding, or data processing. Understanding the Problem Given a string, we want to separate characters based on their position index: Even positions (0, 2, 4...): "T", "i", " ", "s", " ", "a", " ", "e", "t", "!" Odd positions (1, 3, 5...): "h", "s", "i", "", "i", "", "t", "s", "" If the string is "This is a test!" Even positions: "Ti s a ...

Read More

Taking part from array of numbers by percent JavaScript

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

We have an array of number literals like this: const numbers = [10, 6200, 20, 20, 350, 900, 26, 78, 888, 10000, 78, 15000, 200, 1280, 2000, 450]; We need to write a function that takes an array of numbers and a percentage (0-100). The function should return the first n elements from the array that sum up to equal or just less than the specified percentage of the total array sum. Understanding the Problem Let's take a simpler example: const numbers = [12, 10, 6, 8, 4, 2, 8]; ...

Read More

How to do Butterfly Shuffle in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 443 Views

A butterfly shuffled array in JavaScript is an array of Numbers that is sorted such that the numbers decrease as we approach the center of array and increase as we approach the end of array. The biggest number is placed at the very first index. Another variation of butterfly shuffled array is where the numbers increase towards the center and decrease towards the end. In this case the smallest number is placed at the very first index. For people who come from a Mathematics background, it's somewhat relatable to the Gaussian distribution. Example Suppose we have ...

Read More

How to count a depth level of nested JavaScript objects?

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

We have an array of objects, which further have nested objects like this − const arr = [{ id: 0, children: [] }, { id: 1, children: [{ id: 2, children: [] }, { id: 3, children: [{ id: 4, children: [] }] }] }]; ...

Read More

In JavaScript, need to perform sum of dynamic array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 384 Views

Let's say, we have an array that contains the score of some players in different sports. The scores are represented like this − const scores = [ {sport: 'cricket', aman: 54, vishal: 65, jay: 43, hardik: 88, karan:23}, {sport: 'soccer', aman: 14, vishal: 75, jay: 41, hardik: 13, karan:73}, {sport: 'hockey', aman: 43, vishal: 35, jay: 53, hardik: 43, karan:29}, {sport: 'volleyball', aman: 76, vishal: 22, jay: 36, hardik: 24, karan:47}, {sport: 'baseball', aman: 87, vishal: 57, jay: 48, hardik: 69, karan:37}, ]; We need to ...

Read More

Filter the properties of an object based on an array and get the filtered object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 500 Views

In JavaScript, you often need to filter an object's properties based on specific criteria. This article shows how to create a filtered object containing only the properties whose keys appear in a given array. Problem Statement We need to write a function that takes an object and an array of string literals, then returns a new object containing only the key-value pairs where the key exists in the array. For example: If the object is {"a": [], "b": [], "c": [], "d": []} and the array is ["a", "d"], the output should be: {"a": [], ...

Read More
Showing 4541–4550 of 5,340 articles
« Prev 1 453 454 455 456 457 534 Next »
Advertisements