Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 423 of 840
Formatting JavaScript Object to new Array
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 MoreSum from array values with similar key in JavaScript
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 MoreHow to convert an object into an array in JavaScript?
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 MoreHow to add a new object into a JavaScript array after map and check condition?
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 MoreOdd even sort in an array - JavaScript
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 MoreShort Circuit Assignment in JavaScript
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 MoreHow to splice duplicate item in array JavaScript
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 MoreHow to find all subsets of a set in JavaScript?
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 MoreGet only specific values in an array of objects in JavaScript?
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 MoreExplain common code blocks in JavaScript switch statement?
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