AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 443 of 840

How to combine two arrays into an array of objects in JavaScript?

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

Combining two arrays into an array of objects is a common task in JavaScript. This allows you to pair corresponding elements from both arrays into structured data. Using map() with Object Creation The map() method creates a new array by transforming each element. We can use it to combine arrays into objects: var firstArray = ['John', 'David', 'Bob']; var secondArray = ['Mike', 'Sam', 'Carol']; var arrayOfObjects = firstArray.map(function(value, index) { return { first: value, second: ...

Read More

JavaScript Match between 2 arrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 394 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

JavaScript Const

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 351 Views

The JavaScript const declaration creates variables that cannot be reassigned to another value or redeclared later. It was introduced in ES2015 (ES6) and provides block scope like let but with immutable binding. Syntax const variableName = value; Basic const Example body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } Const Example Try ...

Read More

The new.target in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

The new.target is a metaproperty that allows us to determine at runtime whether a function or constructor was called using the new keyword or not. It returns undefined when called as a regular function and references the constructor when called with new. Basic Syntax function MyConstructor() { if (new.target) { // Called with 'new' } else { // Called as regular function } } Example: Detecting Constructor ...

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

JavaScript - Convert an array to key value pair

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

Converting an array of objects to key-value pairs is a common task in JavaScript. This involves transforming structured data into a simple object where one property becomes the key and another becomes the value. Problem Statement Suppose we have an array of student objects like this: const arr = [ {"name": "Rahul", "score": 89}, {"name": "Vivek", "score": 88}, {"name": "Rakesh", "score": 75}, {"name": "Sourav", "score": 82}, {"name": "Gautam", "score": 91}, {"name": "Sunil", ...

Read More

The yield* expression/keyword in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

The yield* expression in JavaScript is used to delegate to another generator function or any iterable object. It yields all values from the delegated generator or iterable, effectively "flattening" nested generators. Syntax function* generatorFunction() { yield* anotherGenerator(); yield* iterableObject; } Basic Example: Delegating to Another Generator yield* Example yield* keyword in JavaScript CLICK ...

Read More

Sum of even numbers from n to m regardless if nm JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 347 Views

We are required to write a function that takes two numbers as arguments m and n, and it returns the sum of all even numbers that falls between m and n (both inclusive). For example: If m = 10 and n = -4 The output should be 10+8+6+4+2+0+(-2)+(-4) = 24 Approach We will first calculate the sum of all even numbers up to n and the sum of all even numbers up to m. Then we will check for the bigger of the two m and n. Subtract the sum of smaller ...

Read More

Returning poker pair cards - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

We are required to write a function that takes in an array of exactly five elements representing the five cards of a poker player drawn randomly. If the five cards contain at least one pair, our function should return the card number of the highest pair (trivial if there only exists a single pair). Else our function should return false. For example: If the array is − const arr = ['A', 'Q', '3', 'A', 'Q']; Then our function should return − 'A' (as 'A' > 'Q' in card games) Card Ranking System ...

Read More

Remove array duplicates by property - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 276 Views

When working with arrays of objects in JavaScript, you often need to remove duplicate entries based on a specific property. This tutorial shows different methods to accomplish this task. The Problem Consider an array of objects with duplicate names: const arr = [ {name: "Jack", age: "14"}, {name: "bob", age: "14"}, {name: "sue", age: "21"}, {name: "Jill", age: "16"}, {name: "Jack", age: "21"} ]; console.log("Original array:"); console.log(arr); Original array: [ ...

Read More
Showing 4421–4430 of 8,392 articles
« Prev 1 441 442 443 444 445 840 Next »
Advertisements