Object Oriented Programming Articles

Page 139 of 589

How to get key name when the value contains empty in an object with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 889 Views

To find keys with empty values in a JavaScript object, you can use Object.keys() combined with find() or filter(). This is useful for form validation or data cleaning. Let's say we have the following object: var details = { firstName: 'John', lastName: '', countryName: 'US' }; Using Object.keys() with find() To find the first key with an empty value, use Object.keys() along with find(): var details = { firstName: 'John', lastName: '', countryName: 'US' ...

Read More

Strictly increasing sequence JavaScript

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

Given a sequence of integers as an array, we have to determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. For example: For sequence = [1, 3, 2, 1], the output should be function(sequence) = false. There is no one element in this array that can be removed in order to get a strictly increasing sequence. For sequence = [1, 3, 2], the output should be function(sequence) = true. You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, ...

Read More

Check if values of two arrays are the same/equal in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 705 Views

We have two arrays of numbers, let's say − [2, 4, 6, 7, 1] [4, 1, 7, 6, 2] Assume, we have to write a function that returns a boolean based on the fact whether or not they contain the same elements irrespective of their order. For example − [2, 4, 6, 7, 1] and [4, 1, 7, 6, 2] should yield true because they have the same elements but ordered differently. Method 1: Using includes() and Length Check This approach checks if both arrays have the same length and every element from ...

Read More

How to convert string type value to array type in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 451 Views

Converting strings to arrays in JavaScript can be done using different methods depending on the string format. Here are the most common approaches. Using JSON.parse() for JSON Strings When you have a JSON-formatted string, use JSON.parse() to convert it to an array: var customerDetails = '[{"name": "John", "countryName": "US"}, {"name": "David", "countryName": "AUS"}, {"name": "Bob", "countryName": "UK"}]'; console.log("Original string:", customerDetails); console.log("Type:", typeof customerDetails); var convertStringToArray = JSON.parse(customerDetails); console.log("After converting to array:"); console.log(convertStringToArray); console.log("Type:", typeof convertStringToArray); Original string: [{"name": "John", "countryName": "US"}, {"name": "David", "countryName": "AUS"}, {"name": "Bob", "countryName": "UK"}] Type: string ...

Read More

Insert value in the middle of every value inside array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 422 Views

In JavaScript, you can insert operation objects between array elements to create alternating patterns. This technique is useful for building mathematical expressions or form builders. Problem Statement We have an array of numbers like this: const numbers = [1, 6, 7, 8, 3, 98]; console.log("Original array:", numbers); Original array: [ 1, 6, 7, 8, 3, 98 ] We need to transform this array into objects with "value" keys, and insert operation objects between them using +, -, *, / alternately. Expected Output Structure The final result should look like ...

Read More

How to move multiple elements to the beginning of the array in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 807 Views

In JavaScript, you can move multiple elements to the beginning of an array by identifying their positions and using array methods like splice() and unshift(). This technique is useful when you need to prioritize certain elements while maintaining the relative order of others. Problem Overview We need to create a function that takes an array and any number of strings as arguments. The function should check if these strings exist in the array and move them to the front while preserving their relative order. Using splice() and unshift() Method This approach removes elements from their current ...

Read More

How to format JSON string in JavaScript?

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

To format JSON string in JavaScript, use JSON.stringify() with spacing parameters. This method converts JavaScript objects to formatted JSON strings with proper indentation for better readability. Syntax JSON.stringify(value, replacer, space) Parameters value - The JavaScript object to convert replacer - Function or array to filter properties (use null for all properties) space - Number of spaces or string for indentation Example var details = { studentId: 101, studentFirstName: 'David', studentLastName: 'Miller', studentAge: 21, subjectDetails: { ...

Read More

JavaScript Match between 2 arrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 394 Views

When working with two arrays in JavaScript, you often need to match elements from one array with corresponding elements in another. This article demonstrates how to extract user IDs from an object array based on a names array, maintaining the order of the names array. Problem Statement We have two arrays: one containing user objects with names and UIDs, and another containing just names. Our goal is to create a function that returns UIDs in the same order as they appear in the names array. const data = [{ name: 'Kamlesh Kapasi', ...

Read More

How to subtract date from today's date in JavaScript?

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

To subtract dates in JavaScript, you can work with Date objects directly or extract specific components like day, month, or year. The most common approach is to subtract one Date object from another to get the difference in milliseconds. Syntax var dateDifference = date1 - date2; // Returns difference in milliseconds var daysDifference = Math.floor((date1 - date2) / (1000 * 60 * 60 * 24)); Example 1: Subtracting Full Dates var currentDate = new Date(); var pastDate = new Date("2024-01-01"); // Get difference in milliseconds var timeDifference = currentDate - pastDate; console.log("Difference ...

Read More

Sort array of points by ascending distance from a given point JavaScript

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

Let's say, we have an array of objects with each object having exactly two properties, x and y that represent the coordinates of a point. We have to write a function that takes in this array and an object with x and y coordinates of a point and we have to sort the points (objects) in the array according to the distance from the given point (nearest to farthest). The Distance Formula It is a mathematical formula that states that the shortest distance between two points (x1, y1) and (x2, y2) in a two-dimensional plane is given by ...

Read More
Showing 1381–1390 of 5,881 articles
« Prev 1 137 138 139 140 141 589 Next »
Advertisements