AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 452 of 840

How to remove an object using filter() in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 659 Views

The filter() method creates a new array with elements that pass a test condition. It's commonly used to remove objects from arrays based on specific criteria. Initial Array Let's start with an array of objects where some have a details property and others don't: let objectValue = [ { "id": "101", "details": { "Name": "John", "subjectName": "JavaScript" }}, { "id": "102", "details": { "Name": "David", "subjectName": "MongoDB" }}, { "id": "103" } ]; console.log("Original array:"); console.log(objectValue); Original array: [ ...

Read More

How to move all capital letters to the beginning of the string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 356 Views

To move all capital letters to the beginning of a string in JavaScript, we can use the sort() method with a regular expression to identify uppercase letters and rearrange them. Let's say we have the following string: my name is JOHN SMITH We'll use sort() along with the regular expression /[A-Z]/ to move all capital letters to the beginning of the string. Example var moveAllCapitalLettersAtTheBeginning = [...'my name is JOHN SMITH'] .sort((value1, value2) => /[A-Z]/.test(value1) ? /[A-Z]/.test(value2) ? 0 : -1 : 0).join(''); console.log("After moving all capital letters at the beginning:"); console.log(moveAllCapitalLettersAtTheBeginning); ...

Read More

Implementing Priority Sort in JavaScript

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

We are required to write a JavaScript function that takes in two arrays of numbers, second being smaller in size than the first. Our function should return a sorted version of the first array (in increasing order) but put all the elements that are common in both arrays to the front. For example − If the two arrays are − const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3]; Then the output should be − [2, 3, 1, 4, 5] How It Works The priority ...

Read More

Are array of numbers equal - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

We need to write a JavaScript function that takes in two arrays of numbers and checks for their equality based on specific criteria. The arrays will be considered equal if: They contain the same elements and in the same order. The product of all the elements of the first array and second array is equal. Example Arrays The first array of numbers: const first = [3, 5, 6, 7, 7]; The second array of numbers: const second = [7, 5, 3, 7, 6]; ...

Read More

Explain focus events in JavaScript

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

Focus events in JavaScript are triggered when HTML elements gain or lose focus on a web page. These events are essential for creating interactive user interfaces and handling form interactions effectively. Focus events occur when users interact with focusable elements like input fields, buttons, or links through clicking, tabbing, or keyboard navigation. Understanding these events helps create responsive and accessible web applications. Types of Focus Events JavaScript provides four main focus events: focus − Triggered when an element gains focus. Does not bubble up the DOM tree. blur − ...

Read More

How to merge objects into a single object array with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 348 Views

In JavaScript, you can merge multiple objects from an array into a single object using various methods. This is useful when you have an array of objects with different properties and want to combine them. Using Object.assign() The Object.assign() method copies properties from source objects to a target object. Using the spread operator with it merges all objects in an array: Merge Objects body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { ...

Read More

Looping in JavaScript to count non-null and non-empty values

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

In JavaScript, counting non-null and non-empty values in an array is a common task. This article demonstrates how to use forEach() to iterate through an array and count valid values. Basic Array Example Let's start with an array containing subject names: let subjectNames = ['JavaScript', 'Angular', 'AngularJS', 'Java']; console.log("Original array:", subjectNames); Original array: [ 'JavaScript', 'Angular', 'AngularJS', 'Java' ] Using forEach() to Count Valid Values The forEach() method executes a function for each array element. Here's the syntax: yourArrayName.forEach(element => { // Your logic ...

Read More

Check if value of an object of certain class has been altered in JavaScript and update another value based on it?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 101 Views

In JavaScript, you can monitor changes to object properties and automatically update dependent values using getters and setters. This approach allows you to create reactive properties that respond when other properties change. Basic Property Monitoring with Getters The simplest approach uses getter methods to access current property values: class Student { constructor(studentMarks1, studentMarks2) { this.studentMarks1 = studentMarks1; this.studentMarks2 = studentMarks2; var self = this; ...

Read More

Does this array contain any majority element - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 177 Views

Given an array of numbers, any element of the array will be a majority element if that element appears more than array length's 1/2 times in the array. For example, if the length of array is 7, then if there's any element in the array that appears for at least 4 number of times, it will be considered a majority. It's quite apparent that any particular array can have at most one majority element. We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns true if there exists ...

Read More

PreventDefault( ) vs Return false in JavaScript?

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

In JavaScript, when handling events, you often need to prevent the default browser behavior. The two common approaches are preventDefault() and return false, but they work differently. The preventDefault() method stops only the default action, while return false stops both the default action and event propagation. Key Differences preventDefault() return false Method available on event objects JavaScript statement that can be used anywhere Stops only the default behavior Stops default behavior AND event propagation Allows event to continue bubbling up Stops event bubbling and function execution ...

Read More
Showing 4511–4520 of 8,392 articles
« Prev 1 450 451 452 453 454 840 Next »
Advertisements