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
Web Development Articles
Page 218 of 801
How to find inside an array of objects the object that holds the highest value in JavaScript?
Finding the object with the highest value in an array of objects is a common task in JavaScript. This example demonstrates how to find the student with the highest grade from an array of student objects. Sample Data Let's start with an array of student objects, where each student has a name and an array of grades: const arr = [ { name: "Student 1", grades: [ 65, 61, 67, 70 ] ...
Read Moreprocess.chdir() Method in Node.js
The process.chdir() method changes the current working directory of the Node.js process. It throws an exception if the operation fails (such as when the specified directory doesn't exist) but returns no value on success. Syntax process.chdir(directory) Parameters directory – A string containing the path to the directory you want to change to. Return Value This method returns undefined on success and throws an error if the directory change fails. Example 1: Successful Directory Change // Node.js program to demonstrate process.chdir() const process = ...
Read MoreHow to merge two object arrays of different size by key in JavaScript
When working with object arrays of different sizes, we often need to merge them based on a common key. This is useful when combining data from different sources that share a common identifier. Suppose we have an object like this: const obj = { "part1": [{"id": 1, "a": 50}, {"id": 2, "a": 55}, {"id": 4, "a": 100}], "part2":[{"id": 1, "b": 40}, {"id": 3, "b": 45}, {"id": 4, "b": 110}] }; We need to merge part1 and part2 arrays to form a single array where objects with the same id ...
Read MoreurlObject.auth() Method in Node.js
The auth property of a parsed URL object contains the username and password portion of a URL, also known as userInfo. The username and password are separated by a colon (:). Syntax urlObject.auth Parameters The auth property is read-only and does not require any input parameters. It returns the authentication credentials from the parsed URL. Example 1: Basic Authentication Create a file named auth.js and run it using node auth.js: // Importing the URL module const url = require('url'); var adr = 'https://username=hello:password=tutorialspoint@www.tutorialspoint.com/'; // Parsing the above URL ...
Read MoreDeep Search JSON Object JavaScript
Deep searching a JSON object means recursively traversing through all nested objects and arrays to find elements that match specific criteria. This is useful when you need to locate objects buried deep within complex data structures. The Problem Suppose we have the following nested JSON object: const obj = { id: 1, title: 'hello world', child: { id: null, title: 'foobar', ...
Read MoreConvert JSON to another JSON format with recursion JavaScript
Suppose, we have the following JSON object − const obj = { "context": { "device": { "localeCountryCode": "AX", "datetime": "3047-09-29T07:09:52.498Z" }, "currentLocation": { "country": "KM", ...
Read MoreSorting Array based on another array JavaScript
Sorting an array based on another array's order is a common JavaScript task. This technique allows you to reorder elements according to a reference array's sequence. Problem Statement Given two arrays, we need to sort the first array based on the order of elements in the second array: const input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"]; console.log("Original array:", input); console.log("Reference order:", sortingArray); Original array: [ 'S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8' ] Reference order: [ 'S-1', ...
Read MoreHow to find and return the longest repeating series of numbers in array with JavaScript
We are required to write a JavaScript function that takes in an array of Numbers that may contain some repeating elements. The function should return the length of the longest repeating number sequence from the array. For example − If the input array is − const arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1]; Then the output should be 3 because the number 2 is repeated 3 times consecutively in the array (and that's the highest number). Using Array.reduce() Method The reduce() method can be used to group ...
Read MoreJavaScript function that takes a multidimensional and a single array, and finds matches of the single array in the multi-d array
We need to write a JavaScript function that takes a multidimensional array (array of arrays) as the first argument and a single array as the second argument. The function should find common elements between each subarray and the single array, returning a new array containing only the matching elements from each subarray. Problem Understanding For each subarray in the multidimensional array, we want to find elements that also exist in the single array. The result preserves the structure but only includes common elements. For example, if we have: const arr1 = [ ...
Read MoreJavaScript group array - find the sets of numbers that can be traveled to using the edges defined
Consider the following input and output arrays: const input = ["0:3", "1:3", "4:5", "5:6", "6:8"]; const output = [ [0, 1, 3], [4, 5, 6, 8] ]; Considering each number as a node in a graph, and each pairing x:y as an edge between nodes x and y, we are required to find the sets of numbers that can be traveled to using the edges defined. In graph theory terms, we need to find the distinct connected components within such a graph. For instance, in the above arrays, there ...
Read More