Object Oriented Programming Articles

Page 133 of 589

Remove elements from array in JavaScript using includes() and splice()?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 416 Views

The includes() method checks whether an array contains a specific element, while splice() is used to add or remove items from an array. Together, they can be used to remove multiple elements from an array efficiently. Syntax array.includes(searchElement) array.splice(start, deleteCount) How It Works The approach involves iterating through the array and using includes() to check if each element should be removed. When a match is found, splice() removes it, and the index is decremented to account for the array shift. Example deleteElementsFromArray = function(elements, ...values) { let elementRemoved ...

Read More

Algorithm to dynamically populate JavaScript array with zeros before and after values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 185 Views

We are given a months array, which contains elements less than 12, where each element will be between 1 and 12 (both inclusive). Our job is to take this array and create a full months array with 12 elements. If the element is present in the original array we use that element, otherwise we use 0 at that place. For example: Input → [5, 7, 9] Output → [0, 0, 0, 0, 5, 0, 7, 0, 9, 0, 0, 0] Now, let's write the code: Using Array.includes() Method const months = [6, ...

Read More

Create HTML Document with Custom URL for the document in JavaScript

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

In JavaScript, you can create an HTML document with a custom URL using document.implementation.createHTMLDocument() and the element. This technique allows you to set a base URL that affects how relative URLs are resolved within the document. How It Works The createHTMLDocument() method creates a new HTML document. By adding a element to the document's head, you establish a base URL that all relative URLs in the document will resolve against. Example Custom Base URL Example ...

Read More

Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 713 Views

We are given an array of numbers and a target sum. Our job is to write a function that returns an array of all the subarrays which add up to the target number using the sliding window algorithm. For example: const arr = [23, 5, 1, 34, 12, 67, 9, 31, 6, 7, 27]; const sum = 40; Should find these subarrays that sum to 40: [ [ 5, 1, 34 ], [ 9, 31 ], [ 6, 7, 27 ] ] The Sliding Window Algorithm The sliding window algorithm ...

Read More

Sort array based on presence of fields in objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 319 Views

In JavaScript, you can sort arrays of objects based on the presence of specific fields using a custom comparator function. This technique is useful when you need to prioritize objects with complete information. Problem Statement Let's say we have an array of people objects where some have both firstName and lastName, some have only one of these properties, and others have neither: const people = [{ firstName: 'Ram', id: 301 }, { firstName: 'Shyam', lastName: 'Singh', ...

Read More

How to run functions iteratively with async await in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 263 Views

Running functions iteratively with async/await allows you to execute asynchronous operations in sequence within loops. This is useful when you need to process items one by one rather than concurrently. Basic Syntax async function iterativeFunction() { for (let i = 0; i < count; i++) { await asyncOperation(); } } Example: Sequential Function Calls async function test(i) { while (i { setTimeout(() => { ...

Read More

How to compare two string arrays, case insensitive and independent about ordering JavaScript, ES6

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 855 Views

We need to write a function that compares two strings to check if they contain the same characters, ignoring case and order. This is useful for validating anagrams or checking string equivalence. For example: const first = 'Aavsg'; const second = 'VSAAg'; isEqual(first, second); // true Using Array Sort Method This method converts strings to arrays, sorts the characters, and compares the results. We extend the String prototype to add a sort method. const first = 'Aavsg'; const second = 'VSAAg'; const stringSort = function(){ return this.split("").sort().join(""); ...

Read More

Method to check if array element contains a false value in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 252 Views

To check if an array element contains a false value in JavaScript, you can use methods like some(), includes(), or Object.values() depending on your data structure. Using some() for Simple Arrays The some() method tests whether at least one element passes a test function: const booleanArray = [true, false, true]; const hasfalseValue = booleanArray.some(value => value === false); console.log("Array contains false:", hasfalseValue); // Or more simply const hasFalse = booleanArray.includes(false); console.log("Using includes():", hasFalse); Array contains false: true Using includes(): true Using Object.values() for Nested Objects For complex nested structures, ...

Read More

Find Max Slice Of Array | JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 279 Views

In JavaScript, finding the maximum slice of an array with at most two different numbers is a common problem that can be efficiently solved using the sliding window technique. This approach maintains a dynamic window that expands and contracts to find the longest contiguous subarray containing no more than two distinct elements. How It Works The sliding window algorithm uses two pointers (start and end) to define a window boundary. We track distinct elements using a map and adjust the window size when the distinct count exceeds two. Example const arr = [1, 1, 1, ...

Read More

Remove leading zeros in a JavaScript array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 478 Views

To remove leading zeros from a JavaScript array, we use the filter() method with a closure function that tracks when the first non-zero element is encountered. Once a non-zero value is found, all subsequent elements (including zeros) are kept. Input Examples [10, 0, 12, 0, 0] [0, 0, 0, 0, 0, 0, 10, 12, 0] [12, 0, 0, 1, 0, 0] Example const removeLeadingZero = input => input.filter((lastValue => value => lastValue = lastValue || value) (false) ); console.log(removeLeadingZero([10, 0, 12, 0, 0])); console.log(removeLeadingZero([0, ...

Read More
Showing 1321–1330 of 5,881 articles
« Prev 1 131 132 133 134 135 589 Next »
Advertisements