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 126 of 534
Finding word starting with specific letter in JavaScript
Finding words that start with a specific letter is a common requirement in JavaScript applications. This guide shows different approaches to locate the first array element beginning with a specified character. Problem Statement We need to write a JavaScript function that takes an array of strings and a character, then returns the index of the first string starting with that character. Using substring() Method The substring() method extracts the first character for comparison: const names = ['Naman', 'Kartik', 'Anmol', 'Rajat', 'Keshav', 'Harsh', 'Suresh', 'Rahul']; const firstIndexOf = (arr = [], char = '') ...
Read MoreMerge two sorted arrays to form a resultant sorted array in JavaScript
We are required to write a JavaScript function that takes in two sorted arrays of numbers. The function should merge the two arrays together to form a resultant sorted array and return that array. For example, if the two arrays are: const arr1 = [2, 6, 6, 8, 9]; const arr2 = [1, 4, 5, 7]; Then the output array should be: [1, 2, 4, 5, 6, 6, 7, 8, 9] Method 1: Using Two Pointers Technique The most efficient approach is the two-pointers technique, which compares elements from both ...
Read MoreHow to merge two arrays with objects in one in JavaScript?
Suppose, we have two arrays of objects like these − const arr1 = [ {name:'test', lastname: 'test', gender:'f'}, {name:'test1', lastname: 'test1', gender:'f'}, {name:'test2', lastname: 'test2', gender:'m'} ]; const arr2 = [ {name:'test21', lastname: 'test21', gender:'f'}, {name:'test1', lastname: 'test1', gender:'f'}, {name:'test2', lastname: 'test2', gender:'m'}, {name:'test22', lastname: 'test22', gender:'m'} ]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ { name: 'test', lastname: 'test', gender: 'f' }, { name: 'test1', lastname: 'test1', gender: 'f' }, { ...
Read MoreRegroup JSON array in JavaScript
Suppose, we have a JSON array of objects like this − const arr = [ { "id": "03868185", "month_10": 6, }, { "id": "03870584", "month_6": 2, }, { "id": "03870584", ...
Read MoreCheck how many objects are in the array with the same key in JavaScript
Suppose, we have an array of objects containing some data about some users like this − const arr = [ { "name":"aaa", "id":"2100", "designation":"developer" }, { "name":"bbb", "id":"8888", "designation":"team lead" }, { "name":"ccc", "id":"6745", ...
Read MorePartition N where the count of parts and each part are a power of 2, and part size and count are restricted in JavaScript
We need to write a JavaScript function that partitions a number into chunks following specific rules: The number of chunks must be a power of 2 (1, 2, 4, 8, 16, etc.) Each chunk size must also be a power of 2, with a maximum limit Understanding the Problem Let's examine how different numbers can be partitioned: For number 8: [8] This works because we have 1 chunk (power of 2) with size 8 (power of 2). For number 9: [8, 1] This works because we have 2 chunks (power of 2), each ...
Read MoreFlat array of objects to tree in JavaScript
Converting a flat array of objects into a tree structure is a common requirement in web development. This involves organizing objects with parent-child relationships into a hierarchical format. The Data Structure We start with a flat array where each object has an id, name, and parentId. Objects with parentId: null are root nodes, while others are children. .parent, .child { cursor: pointer; ...
Read MoreJavaScript Bubble sort for objects in an array
Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list and swapping adjacent elements if they are in the wrong order. When applied to an array of objects, we can sort based on specific properties. The Shoe Class Let's start with a constructor class that creates Shoe objects: class Shoe { constructor(name, price, type) { this.name = name; this.price = price; this.type = ...
Read MoreConverting a comma separated string to separate arrays within an object JavaScript
When working with comma-separated strings containing hierarchical data, we often need to parse them into structured objects. This article shows how to convert a string like 'dress/cotton/black, dress/leather/red' into an organized object with arrays. The Problem Suppose we have a string like this: const str = 'dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james'; We need to create a JavaScript function that converts this into an object where each category becomes a key, and all related values are grouped into arrays: const output = { dress: ["cotton", "black", "leather", "red", "fabric"], ...
Read MoreCount number of entries in an object having specific values in multiple keys JavaScript
In JavaScript, you often need to count objects in an array that match specific criteria across multiple properties. This is commonly done using the filter() method combined with conditional logic. Suppose we have an array of objects representing trade data: const arr = [ {"goods":"Wheat", "from":"GHANA", "to":"AUSTRALIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, ]; We need to count objects where the "from" property equals "USA" and the "to" property equals "INDIA". Using filter() and length ...
Read More