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
Javascript Articles
Page 457 of 534
Recursively flat an object JavaScript
We are required to write a function that flattens nested objects by converting deeply nested properties into dot-notation keys. This is useful for data transformation and API processing. If the input object is: const input = { a: 0, b: {x: {y: 1, z: 2}}, c: 3 }; Then the output of the function should be: const output = { a: 0, 'b.x.y': 1, 'b.x.z': 2, c: 3 } Recursive ...
Read MoreHow do I write a function that takes an array of values and returns an object JavaScript?
Let's say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types. For example − // if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; // then the output Map should be: Map(5) { 'string' => [ 'class', 'name' ], 'number' => [ 2, 6 ], 'object' => [ [ 7, 8, 9 ], { name: 'Michael' ...
Read MoreSplit one-dimensional array into two-dimensional array JavaScript
We are required to write a function that takes in a one-dimensional array as the first argument and a number n as the second argument and we have to make n subarrays inside of the parent array (if possible) and divide elements into them accordingly. If the array contains 9 elements and we asked to make 4 subarrays, then dividing 2 elements in each subarray creates 5 subarrays and 3 in each creates 3, so in such cases we have to fallback to nearest lowest level (3 in ...
Read MoreSort array based on another array in JavaScript
We often need to sort an array based on the order specified in another array. This technique is useful when you want certain elements to appear first while maintaining the original order of remaining elements. For example, we have an original array and want elements from a sortOrder array to appear at the beginning: const originalArray = ['Apple', 'Cat', 'Fan', 'Goat', 'Van', 'Zebra']; const sortOrder = ['Zebra', 'Van']; Using Custom Sort Function We can create a custom comparator function that prioritizes elements present in the sortOrder array: const originalArray = ['Apple', 'Cat', ...
Read MoreFilter an object based on an array JavaScript
Let's say we have an array and an object like this: const arr = ['a', 'd', 'f']; const obj = { "a": 5, "b": 8, "c": 4, "d": 1, "e": 9, "f": 2, "g": 7 }; We are required to write a function that takes in the object and the array and filter away all the object properties that are not an element of the array. So, the output should only contain 3 properties, namely: "a", "d" and "f". Method 1: ...
Read MoreWhat should be the correct Algorithm to Get Array B from Array A counting backwards from the last element in JavaScript?
Consider the following binary array (Array A) − const arr = [1, 0, 1, 1, 1, 1, 0, 1, 1]; When this array is passed through the function, say sumRight(), it produces the following output array (Array B) − const output = [1, 0, 4, 3, 2, 1, 0, 2, 1]; Understanding the Algorithm Elements in array arr can be either 0 or 1. The function counts backward from the last element of array arr, if there are consecutive 1's in the array arr then the corresponding element in the output ...
Read MoreJavaScript Return the lowest index at which a value should be inserted into an array once it has been sorted (either in ascending or descending order).
We have to write a function that returns the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted (either in ascending or descending order). The returned value should be a number. For example, Let's say, we have a function getIndexToInsert() − getIndexToInsert([1, 2, 3, 4], 1.5, 'asc') should return 1 because it is greater than 1 (index 0), but less than 2 (index 1). Likewise, getIndexToInsert([20, 3, 5], 19, 'asc') should return 2 because once the array has been sorted in ...
Read MoreHow to remove every Nth element from an array JavaScript?
Removing every Nth element from an array is a common task in JavaScript. This can be accomplished using the Array.prototype.splice() method to modify the array in-place, or by creating a new array with the filtered elements. Method 1: Using splice() (In-Place Modification) The splice() method removes elements from the array and modifies the original array: const arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']; console.log("Original array:", arr); const removeNth = (arr, n) => { // Start from n-1 index and remove every nth element ...
Read MoreExtract properties from an object in JavaScript
We have to write a JavaScript function, say extract() that extracts properties from an object to another object and then deletes them from the original object. For example − If obj1 and obj2 are two objects, then obj1 = {color:"red", age:"23", name:"cindy"} obj2 = extract(obj1, ["color", "name"]) After passing through the extract function, they should become like − obj1 = { age:23 } obj2 = {color:"red", name:"cindy"} Therefore, let's write the code for this function − Syntax const extract = (obj, ...keys) => { ...
Read MoreConvert an array of binary numbers to corresponding integer in JavaScript
Let's say, we have an array of Numbers that contains 0 and 1 − const arr = [0, 1, 0, 1]; We are required to write an Array function, toBinary() that returns the corresponding decimal integer for the array it is used with. For example − If the array is − const arr = [1, 0, 1, 1]; Then the output should be 11 because the decimal representation of binary 1011 is 11. Therefore, let's write the code for this function. Method 1: Using parseInt() with join() In ...
Read More