Javascript Articles - Page 2 of 607

How to Deep Merge Two Objects in JavaScript?

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

1K+ 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

2K+ 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

617 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 Last Day of Previous Month from Date in Moment.JS?

AYUSH MISHRA
Updated on 20-Dec-2024 10:11:27

5K+ Views

Sometimes while working with with dates in JavaScript, it is common to need specific dates for calculations or validations. One such situation is when we are given a date and we have to determine the last day of the previous month. In this article, we are going to learn how we can last day of the Previous Month from Date in 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. We can install the Moment.js library in ... Read More

How to Filter Key of an Object using Lodash?

AYUSH MISHRA
Updated on 12-Dec-2024 09:15:04

7K+ Views

In JavaScript, sometimes we need some specific key-value pairs from an object. Suppose we have a large object and only want to select some specific keys. In this article, we will learn how to filter the keys of an object using Lodash. What is Lodash? Lodash is a popular JavaScript library that provides 200+ functions to facilitate web development. It provides helper functions like map, filter, and invoke as well as function binding, javascript templating, deep equality checks, creating indexes, and so on. Lodash can be used directly inside a browser and also with Node.js. Lodash Installation There are ... Read More

JavaScript Program to Convert Radians to Degree

AYUSH MISHRA
Updated on 15-Nov-2024 12:18:14

15K+ Views

Radians and degrees are units of angular measurement used mainly in mathematics, physics, and engineering. Converting radians to degrees is important, as it is a common operation in trigonometry and other mathematical calculations. In this article, we will learn how to convert Radians to Degrees using JavaScript. .tp-container { padding: 2rem; max-width: 400px; max-width: fit-content; margin-left: auto; margin-right: auto; } .input-group { margin-bottom: 1rem; } .tp-input { width: 100%; ... Read More

How to Convert JSON to ArrayBuffer in JavaScript?

Pankaj Kumar Bind
Updated on 14-Nov-2024 13:31:50

1K+ Views

In JavaScript, an ArrayBuffer is a kind of binary structure that enables the manipulation of unprocessed binary. While there is a representation of data with the use of strings such as JSON, it is sometimes useful to indicate that JSON could be transformed into an ArrayBuffer especially when interacting with binary APIs or when there is a need to compress data areas. In this article, we will focus on three widespread methods of converting a JSON object into an ArrayBuffer in JavaScript. Approaches to Convert JSON to ArrayBuffer Using TextEncoder: Suitable for UTF-8 encoding. ... Read More

How to Create Protected Object Properties in JavaScript?

Pankaj Kumar Bind
Updated on 14-Nov-2024 12:46:09

1K+ Views

JavaScript does not have a specific attribute protection level like the one that exists in some other languages, but we can come close using a few techniques. Protected properties are those that are accessible only to certain methods of an object and are not exposed to outside access. This idea is helpful when securing information against illegitimate access since only specific functions or methods can read or alter it. Approaches to Create Protected Object Properties Using Closures Using WeakMap Using Closures Closures in JavaScript can encapsulate properties and ... Read More

Advertisements