Object Oriented Programming Articles

Page 137 of 589

Merge objects in array with similar key JavaScript

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

When working with arrays of objects in JavaScript, you might need to merge objects that share a common key. This is useful when consolidating data from multiple sources or restructuring existing data. Let's say we have the following array of objects where each object has an id and various heading properties: const arr = [ {id: 1, h1: 'Daily tests'}, {id: 2, h1: 'Details'}, {id: 1, h2: 'Daily classes'}, {id: 3, h2: 'Results'}, {id: 2, h3: 'Admissions'}, ...

Read More

How to remove certain number elements from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 548 Views

We are required to write a function that takes in an array of numbers and a number, and it should remove all the occurrences of that number from the array in-place. Let's explore different approaches to accomplish this task effectively. Method 1: Using Recursion with splice() We can use recursion to remove elements by finding and splicing them one by one. The recursive function continues until no more occurrences are found. const numbers = [1, 2, 0, 3, 0, 4, 0, 5]; const removeElement = (arr, element) => { if(arr.indexOf(element) !== ...

Read More

Get the index of the nth item of a type in a JavaScript array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 752 Views

When working with JavaScript arrays, you might need to find the index of the nth occurrence of a specific value. This is useful for parsing data, finding patterns, or processing structured arrays. Problem Statement We need to write a function getIndex() that takes three parameters: an array arr, a value txt (string or number), and a number n. The function should return the index of the nth appearance of txt in arr. If txt doesn't appear n times, return -1. Using Array.reduce() Method The reduce() method provides an elegant solution by maintaining a counter and tracking ...

Read More

Sort a JavaScript array so the NaN values always end up at the bottom.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 661 Views

We have an array that contains String and number mixed data types, we have to write a sorting function that sorts the array so that the NaN values always end up at the bottom. The array should contain all the normal numbers first followed by string literals and then followed by NaN numbers. We know that the data type of NaN is "number", so we can't check for NaN like !number && !string. Moreover, if we simply check the tautology and falsity of elements then empty strings will also satisfy the same condition which NaN or undefined satisfies. ...

Read More

How to Replace null with "-" JavaScript

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

In JavaScript, you often need to replace null, undefined, empty strings, and other falsy values with a default placeholder like "-". This is common when displaying data in tables or forms where empty values should show a dash instead of being blank. Understanding Falsy Values JavaScript considers these values as falsy: null, undefined, '' (empty string), 0, NaN, and false. We can use this to our advantage when replacing them. Method 1: Using Object.keys() and forEach() This approach iterates through all object keys and replaces falsy values in place: const obj = { ...

Read More

How to put variable in regular expression match with JavaScript?

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

You can put a variable in a regular expression match by using the RegExp constructor, which accepts variables as patterns. This is essential when you need dynamic pattern matching in JavaScript. Syntax // Using RegExp constructor with variable let pattern = new RegExp(variableName, flags); string.match(pattern); // Or directly with match() string.match(variableName); // for simple string matching Example: Using Variable in Regular Expression let sentence = 'My Name is John'; console.log("The actual value:"); console.log(sentence); let matchWord = 'John'; console.log("The matching value:"); console.log(matchWord); // Using RegExp constructor with variable let matchRegularExpression = ...

Read More

Generate colors between #CCCCCC and #3B5998 for a color meter with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 355 Views

We have to write a function that generates a random color between two given colors. Let's tackle this problem in parts − First → We write a function that generates a random number between two given numbers. Second → Instead of using the hex scale for random color generation, we will map the hex to 0 to 15 decimal scale and use that instead. ...

Read More

Recursively loop through an array and return number of items with JavaScript?

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

We need to write a function that recursively searches through a nested array and counts how many times a specific value appears. This is useful when working with complex nested data structures. Problem Statement Given a nested array, we want to count all occurrences of a search term, including items in nested sub-arrays. const names = ["rakesh", ["kalicharan", "krishna", "rakesh", "james", ["michael", "nathan", "rakesh", "george"]]]; For the above array, searching for "rakesh" should return 3 because it appears once at the top level and twice in nested arrays. Recursive Solution Here's a ...

Read More

Check if a string has white space in JavaScript?

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

To check if a string contains whitespace in JavaScript, you can use several methods. The most common approaches are indexOf(), includes(), and regular expressions. Using indexOf() Method The indexOf() method returns the index of the first whitespace character, or -1 if none is found: function stringHasTheWhiteSpaceOrNot(value){ return value.indexOf(' ') >= 0; } var whiteSpace = stringHasTheWhiteSpaceOrNot("MyNameis John"); if(whiteSpace == true){ console.log("The string has whitespace"); } else { console.log("The string does not have whitespace"); } // Test with different strings console.log(stringHasTheWhiteSpaceOrNot("HelloWorld")); ...

Read More

Write an algorithm that takes an array and moves all of the zeros to the end JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 358 Views

We need to write a function that moves all zeros in an array to the end while maintaining the order of non-zero elements. This algorithm should work in-place without using extra space. Problem Overview Given an array with mixed numbers including zeros, we want to push all zeros to the end while keeping other elements in their relative order. For example, [1, 0, 3, 0, 5] should become [1, 3, 5, 0, 0]. Method 1: Using splice() and push() This approach removes zeros when found and adds them to the end: const arr = ...

Read More
Showing 1361–1370 of 5,881 articles
« Prev 1 135 136 137 138 139 589 Next »
Advertisements