Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 18 of 589
How to allocate memory to an object whose length is set to 0 - JavaScript?
In JavaScript, you can manipulate array length to clear memory and allocate new slots. When you set an array's length to 0, it clears all elements. Setting length to a larger value allocates empty slots. Understanding Array Length Property The length property controls how many elements an array can hold. Setting it to 0 removes all elements, while setting it to a larger number creates empty slots filled with undefined. var arrayObject = [ "John", "David", "Mike" ]; console.log("Original array:", arrayObject); console.log("Original ...
Read MoreFlatten array to 1 line in JavaScript
Suppose, we have a nested array of numbers like this − const arr = [ [ 0, 0, 0, -8.5, 28, 8.5 ], [ 1, 1, -3, 0, 3, 12 ], [ 2, 2, -0.5, 0, 0.5, 5.3 ] ]; We are required to write a JavaScript function that takes in one such nested array of numbers. The function should combine all the numbers in the nested array to form a single string. In the resulting string, the adjacent numbers should be separated by whitespaces and ...
Read MoreFetch specific values from array of objects in JavaScript?
Fetching specific values from an array of objects is a common JavaScript task. This article demonstrates multiple approaches to filter and extract data based on specific criteria. Sample Data Let's start with an array of employee objects: const details = [ { employeeFirstName: "John", employeeLastName: "Doe" }, { employeeFirstName: "David", employeeLastName: "Miller" ...
Read MoreMap multiple properties in array of objects to the same array JavaScript
When working with arrays of objects in JavaScript, you often need to extract multiple property values and combine them into a single flat array. This tutorial shows different approaches to map multiple properties from an array of objects. Problem Statement Given an array of objects like this: const arr = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6} ]; We need to extract all property values and create a flat array: const output = [1, 2, 3, 4, ...
Read MoreEnter values with prompt and evaluate on the basis of conditions in JavaScript?
JavaScript's prompt() function allows you to collect user input and evaluate it using conditional statements. This is useful for creating interactive web applications that respond based on user-provided values. Basic Syntax To get user input and convert it to a number: var value = parseInt(prompt("Enter a value")); Example: Conditional Evaluation Based on User Input Here's a complete example that prompts for two values and evaluates their product: Prompt and Evaluate ...
Read MoreFiltering array within a limit JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and an upper limit and lower limit number as second and third argument respectively. Our function should filter the array and return a new array that contains elements between the range specified by the upper and lower limit (including the limits). Syntax const filterByLimits = (arr, upper, lower) => { return arr.filter(element => element >= lower && element { let res = []; res = arr.filter(el => { return el >= lower && el { return arr.filter(element => element >= lower && element = lower && element
Read MoreRemove/ filter duplicate records from array - JavaScript?
JavaScript arrays often contain duplicate records that need to be removed. There are several effective methods to filter duplicates, depending on whether you're working with primitive values or objects. Sample Data with Duplicates Let's start with an array of nationality objects containing duplicate values: var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ]; console.log("Original array:", objectOfNationality); ...
Read MoreConstruct objects from joining two strings JavaScript
We are required to write a JavaScript function that takes in two comma separated strings. The first string is the key string and the second string is the value string, the number of elements (commas) in both the strings will always be the same. Our function should construct an object based on the key and value strings and map the corresponding values to the keys. Example const str1= '[atty_hourly_rate], [paralegal_hourly_rate], [advanced_deposit]'; const str2 = '250, 150, 500'; const mapStrings = (str1 = '', str2 = '') => { const keys = ...
Read MoreNumber prime test in JavaScript by creating a custom function?
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In JavaScript, we can create a custom function to check if a number is prime. Basic Prime Number Function Here's a simple approach to check if a number is prime: function checkNumberIsPrime(number) { if (number
Read MoreFinding the longest valid parentheses JavaScript
Given a string containing just the characters '(' and ')', we find the length of the longest valid (well-formed) parentheses substring. A set of parentheses qualifies to be a well-formed parentheses, if and only if, for each opening parentheses, it contains a closing parentheses. For example: '(())()' is a well-formed parentheses '())' is not a well-formed parentheses '()()()' is a well-formed parentheses Algorithm Approach The solution uses a stack-based approach to track invalid parentheses positions. We push indices of unmatched characters onto the stack, then calculate the longest valid substring between these invalid ...
Read More