AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 423 of 840

Formatting JavaScript Object to new Array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 578 Views

In JavaScript, you can format objects into arrays using various methods like destructuring, Object.keys(), Object.values(), or custom formatting based on your needs. This is useful for data transformation and display purposes. Example: Formatting Object Properties to Array Format Object to Array body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

Sum from array values with similar key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 603 Views

When working with arrays of data, you often need to group and sum values based on a common key. This article demonstrates how to sum array values with similar keys using JavaScript's reduce() method. Let's say we have an array containing stock transaction data for a company over time: const transactions = [ ['AAPL', 'buy', 100], ['WMT', 'sell', 75], ['MCD', 'buy', 125], ['GOOG', 'sell', 10], ['AAPL', 'buy', 100], ['AAPL', 'sell', 100], ...

Read More

How to convert an object into an array in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 479 Views

In JavaScript, there are several methods to convert an object into an array. The approach depends on whether you want the keys, values, or both from the object. Using Object.keys() to Get Keys Array The Object.keys() method returns an array of an object's property names: const student = { name: "Chris", age: 25, marks: 85, city: "New York" }; const keysArray = Object.keys(student); console.log(keysArray); [ 'name', 'age', 'marks', 'city' ] Using Object.values() to Get ...

Read More

How to add a new object into a JavaScript array after map and check condition?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 468 Views

In JavaScript, you can add new objects or properties to an array after filtering and mapping by combining filter(), map(), and array manipulation techniques. Understanding the Approach The technique involves first filtering data based on a condition, then adding computed properties or objects derived from mapping operations on the original data. Example: Adding Property to Filtered Array const details = [ { customerName: 'John', customerCountryName: 'UK', isMarried: true }, { customerName: 'David', customerCountryName: 'AUS', isMarried: false }, { customerName: 'Mike', customerCountryName: 'US', isMarried: ...

Read More

Odd even sort in an array - JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order. For example: If the input array is − const arr = [2, 5, 2, 6, 7, 1, 8, 9]; Then the output should be − const output = [2, 2, 6, 8, 1, 5, 7, 9]; Approach The solution uses a custom comparator function that: Places even numbers ...

Read More

Short Circuit Assignment in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 263 Views

Short circuit assignment in JavaScript uses logical operators (|| and &&) to assign values based on boolean evaluation. These operators stop evaluating as soon as the result is determined. How Short Circuit Assignment Works The || (OR) operator returns the first truthy value it encounters, while && (AND) returns the first falsy value or the last value if all are truthy. Example with OR Operator (||) Short Circuit Assignment Short Circuit Assignment ...

Read More

How to splice duplicate item in array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 720 Views

We have an array of Number / String literals that contain some duplicate values, we have to remove these values from the array without creating a new array or storing the duplicate values anywhere else. We will use the Array.prototype.splice() method to remove entries in-place, and we will take help of Array.prototype.indexOf() and Array.prototype.lastIndexOf() method to determine the duplicacy of any element. The Problem with Forward Iteration When using forEach() to remove duplicates, we encounter index shifting issues. Here's why the basic approach doesn't work perfectly: const arr = [1, 4, 6, 1, 2, 5, ...

Read More

How to find all subsets of a set in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 736 Views

To find all subsets of a set in JavaScript, you can use the reduce() method along with map() to generate all possible combinations. A subset is any combination of elements from the original set, including the empty set and the set itself. How It Works The algorithm starts with an empty subset [[]] and for each element in the original array, it creates new subsets by adding that element to all existing subsets. Example const findAllSubsetsOfGivenSet = originalArrayValue => originalArrayValue.reduce( (givenSet, setValue) ...

Read More

Get only specific values in an array of objects in JavaScript?

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

When working with arrays of objects in JavaScript, you often need to extract specific values based on certain criteria. This can be accomplished using several array methods like filter(), map(), and find(). Let's say we have the following array of student objects: var details = [{ studentName: "John", studentMarks: 92 }, { studentName: "David", studentMarks: 89 }, { studentName: "Mike", studentMarks: 98 }]; Using filter() to Get Objects Meeting Criteria ...

Read More

Explain common code blocks in JavaScript switch statement?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 319 Views

Common code blocks in JavaScript switch statements allow multiple cases to share the same execution code. This is achieved by omitting the break statement, causing execution to "fall through" to subsequent cases. What are Common Code Blocks? Common code blocks occur when multiple case values need to execute the same code. Instead of duplicating code, you can group cases together by omitting break statements. Syntax switch (expression) { case value1: case value2: case value3: // ...

Read More
Showing 4221–4230 of 8,392 articles
« Prev 1 421 422 423 424 425 840 Next »
Advertisements