AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 394 of 840

Splitting strings based on multiple separators - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 667 Views

We are required to write a JavaScript function that takes in a string and any number of characters specified as separators. Our function should return a splitted array of the string based on all the separators specified. For example, if the string is: const str = 'rttt.trt/trfd/trtr, tr'; And the separators are: const sep = ['/', '.', ', ']; Then the output should be: const output = ['rttt', 'trt', 'trfd', 'trtr']; Method 1: Using Manual Iteration This approach manually iterates through each character and checks if ...

Read More

How do I search through an array using a string, which is split into an array with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 276 Views

We are given an array of strings and another string for which we are required to search in the array. We can filter the array checking whether it contains all the characters that user provided through the input. Using Array Filter with Split (Flexible Search) This approach splits the search string into parts and checks if each part exists in the array elements, regardless of order. const deliveries = ["14/02/2020, 11:47, G12, Kalkaji", "13/02/2020, 11:48, A59, Amar Colony"]; const input = "g12, kal"; const pn = input.split(" "); const requiredDeliveries = deliveries.filter(delivery => ...

Read More

JavaScript creating an array from JSON data?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 986 Views

To create an array from JSON data in JavaScript, you can use the map() method to extract specific values from each object. This is particularly useful when working with API responses or complex data structures. Basic JSON Data Structure Let's start with a simple JSON array containing student information: const studentDetails = [ { name: "John" }, { name: "David" }, ...

Read More

Can someone explain to me what the plus sign is before the variables in JavaScript?

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

The plus (+) sign before a variable in JavaScript is the unary plus operator. It converts the value to a number, making it useful for type conversion from strings, booleans, or other data types to numeric values. Syntax +value How It Works The unary plus operator attempts to convert its operand to a number. It works similarly to Number() constructor but with shorter syntax. Example: String to Number Conversion var firstValue = "1000"; console.log("The data type of firstValue = " + typeof firstValue); var secondValue = 1000; console.log("The data type ...

Read More

Fetching JavaScript keys by their values - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

In JavaScript, you can find an object key by its value using several approaches. This is useful when you need to perform reverse lookups in objects where both keys and values are unique. Consider this object where we want to find keys by their corresponding values: const products = { "Pineapple": 38, "Apple": 110, "Pear": 109 }; console.log(products); { Pineapple: 38, Apple: 110, Pear: 109 } Using Object.keys() with find() The most straightforward approach uses Object.keys() with find() to locate the ...

Read More

Sorting arrays by two criteria in JavaScript

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

When sorting arrays by multiple criteria in JavaScript, you need to create a custom comparator function that handles each condition sequentially. This article demonstrates sorting objects by priority (isImportant) first, then by date within each priority group. The Array Structure Consider an array of objects with id, date, and isImportant properties. We want important items first, then sort by date within each group: const array = [{ id: 545, date: 591020824000, isImportant: false, }, { id: 322, ...

Read More

Filter array of objects by a specific property in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 620 Views

Filtering arrays of objects by specific properties is a common task in JavaScript. While the article shows using map() with a ternary operator for comparison, the filter() method is typically more appropriate for filtering operations. Basic Array Filtering with filter() The filter() method creates a new array with elements that pass a test condition: let customers = [ {firstName: 'John', amount: 100}, {firstName: 'David', amount: 50}, {firstName: 'Bob', amount: 80}, {firstName: 'Alice', amount: 120} ]; // Filter customers with ...

Read More

Split a URL in JavaScript after every forward slash?

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

To split a URL in JavaScript, use the split() method with a forward slash (/) as the delimiter. This breaks the URL into an array of segments. Syntax url.split("/") Basic Example var newURL = "http://www.example.com/index.html/homePage/aboutus/"; console.log("Original URL:", newURL); var splitURL = newURL.split("/"); console.log("Split URL:", splitURL); Original URL: http://www.example.com/index.html/homePage/aboutus/ Split URL: [ 'http:', '', 'www.example.com', 'index.html', 'homePage', 'aboutus', '' ] Understanding the Output The result contains empty strings because: Index 0: 'http:' ...

Read More

Checking an array for palindromes - JavaScript

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

We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array. A palindrome is a word, number, or sequence that reads the same forward and backward. For example, "dad", "racecar", and 12321 are palindromes. Problem Statement If the input array is: const arr = ['carecar', 1344, 12321, 'did', 'cannot']; Then the output should be: const output = [12321, 'did']; Approach We will create a helper function ...

Read More

Sorting by 'next' and 'previous' properties (JS comparator function)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 434 Views

When working with linked data structures in JavaScript, you may encounter objects that reference each other through 'next' and 'previous' properties. This tutorial demonstrates how to sort such objects into their correct sequential order. Problem Statement Consider an array of objects representing pages of a website, where each object has: An id property for unique identification A next property pointing to the next page's id (unless it's the last page) A previous property pointing to the previous page's id (unless it's the first page) These objects are randomly positioned in the array, and we need ...

Read More
Showing 3931–3940 of 8,392 articles
« Prev 1 392 393 394 395 396 840 Next »
Advertisements