Found 6710 Articles for Javascript

JavaScript program to find the average of all negative numbers in an array

AYUSH MISHRA
Updated on 22-Jan-2025 11:06:46

6K+ Views

In this problem, we are given an array of integers, which may contain both negative and positive numbers. We have to find the average of all the negative numbers in the array. In this article, we are going to learn how we can find the average of all negative numbers in an array using JavaScript. Example 1 Input arr = [1, -3, 4, -2, -5]; Output -3.33 Explanation The negative numbers in the array are -3, -2, and -5. Their sum is -10, and the number of negative elements is 3. The average is: -10 / 3 = -3.33. ... Read More

How to Deep Merge Two Objects in JavaScript?

Pankaj Kumar Bind
Updated on 06-Jan-2025 10:42:06

844 Views

In JavaScript, a deep merge of two objects means creating a new object by combining properties recursively, and adding nested objects. Unlike in a simple merge, it ensures that no properties are lost during the merge, which replaces the top-level properties. In this article, we will look at several ways of performing a deep merge of two objects, with code and explanations. Approaches to Deep Merge Manual Recursive Function Using Lodash's merge Method Using the Spread Operator with Recursion Using Object.assign with ... Read More

How to Run cmd.exe with parameters from JavaScript?

Pankaj Kumar Bind
Updated on 03-Jan-2025 11:06:17

1K+ Views

Running cmd.exe with parameters from JavaScript typically involves using Node.js because standard JavaScript running in the browser does not have access to the system's command line for security reasons. With Node.js, you can use the child_process module to execute cmd.exe or any command with parameters. Approaches to Run cmd.exe with Parameters Using Node.js to Run cmd.exe with Parameters Passing Parameters Dynamically Using Spawn for Advanced Scenarios Using Node.js to Run cmd.exe with Parameters cmd.exe /c: Runs the command specified ... Read More

How to Convert JavaScript Class to JSON in JavaScript?

Pankaj Kumar Bind
Updated on 03-Jan-2025 09:35:12

425 Views

JavaScript has a class mechanism, primarily a blueprint for defining objects, including their properties and methods. Data must always be serialized to either store it, transmit it through a network or be used as part of the response to an API call. In this article, we will demonstrate how this can be done using JavaScript by providing examples and outputs as necessary. Approaches Convert JavaScript Class to JSON Using JSON.stringify() Method Adding a Custom toJSON() Method Using Object.assign() Method Using Custom Serializer ... Read More

How to Filter Object by Values in Lodash?

AYUSH MISHRA
Updated on 31-Dec-2024 10:36:43

5K+ Views

Sometimes we are given an object and we want to extract only the key-value pairs that meet certain criteria. In this article, we are going to discuss how we can filter objects by values in Lodash. Prerequisite Lodash: Lodash is a popular JavaScript library used to deal with a variety of functions such as arrays, objects, strings, and more. Install Lodash We can install the Lodash library in the project by adding it via CDN or npm using the following command: npm install lodash Approaches to Filter Object by Values ... Read More

How to Filter Object by Keys in Lodash?

AYUSH MISHRA
Updated on 30-Dec-2024 09:31:39

6K+ Views

Sometimes while working with objects in JavaScript we want to filter objects based on keys. We can easily do this by using Lodash, a popular JavaScript library that provides several inbuilt functions to achieve this easily. In this article, we are going to discuss how we can Filter Objects by Keys using Lodash. Prerequisite Lodash: Lodash is a popular JavaScript library used to deal with a variety of functions such as arrays, objects, strings, and more. Install Lodash We can install the Lodash library in the project by adding it via CDN or ... Read More

How to Get the Beginning and End of the Day with MomentJS?

AYUSH MISHRA
Updated on 30-Dec-2024 09:14:01

7K+ Views

Sometimes, we need to find the beginning and end of the day using MomentJS. This functionality has different real-life applications, such as filtering data within a day’s range, generating daily reports, and scheduling tasks at specific times. In this article, we will discuss how to find the beginning and end of the day using MomentJS. Prerequisite Moment.js: Moment.js is a popular JavaScript library used to deal with dates. This library is used to modify, parse, validate, manipulate, and display dates and times in JavaScript. Install Moment.js We can install the Moment.js library in ... Read More

How to convert MomentJS Date to Users Local Timezone?

AYUSH MISHRA
Updated on 24-Dec-2024 10:08:08

4K+ Views

Sometimes, when doing a project for a specific organization in a particular country, we need to convert it into the users' local timezone. In this article, we are going to learn how to convert a date to a user's local timezone using moment.js. Prerequisite Moment.js: Moment.js is a popular JavaScript library used for dealing with dates. This library is used to modify, parse, validate, manipulate, and display dates and times in JavaScript. MomentJS works with UTC (Coordinated Universal Time) by default or any other specific time zone. Install Moment.js: npm install moment ... Read More

How to Subtract One Month using MomentJS?

AYUSH MISHRA
Updated on 23-Dec-2024 11:04:35

5K+ Views

Sometimes while working with with dates in JavaScript, it is common to need specific dates for calculations or validations. In this problem, we are given a date in the form of year-month-date and we have to subtract one month from it. In this article, we are going to learn how we can subtract one month from a given date using Moment.JS. Prerequisite Moment.js: Moment.js is a popular JavaScript library used for dealing with dates. This library is used to modify, parse, validate, manipulate, and display dates and times in JavaScript. Install Moment.js We ... Read More

How to set 00:00:00 using MomentJS?

AYUSH MISHRA
Updated on 23-Dec-2024 11:03:36

6K+ Views

Sometimes while working with dates and times, we need to manipulate or normalize them to a specific time. One such common need is to reset a date's time to the start of the day, i.e. 00:00:00 midnight. In this article, we are going to learn different approaches to set the time to 00:00:00 using Moment.js. Prerequisite Moment.js: Moment.js is a popular JavaScript library used to deal with dates. This library is used to modify, parse, validate, manipulate, and display dates and times in JavaScript. Install Moment.js We ... Read More

Advertisements