Object Oriented Programming Articles

Page 141 of 589

Filter the properties of an object based on an array and get the filtered object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 506 Views

In JavaScript, you often need to filter an object's properties based on specific criteria. This article shows how to create a filtered object containing only the properties whose keys appear in a given array. Problem Statement We need to write a function that takes an object and an array of string literals, then returns a new object containing only the key-value pairs where the key exists in the array. For example: If the object is {"a": [], "b": [], "c": [], "d": []} and the array is ["a", "d"], the output should be: {"a": [], ...

Read More

Simplest code for array intersection in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 225 Views

Array intersection finds common elements between two or more arrays. In JavaScript, the simplest approach uses filter() combined with includes(). Basic Example Let's find common elements between two arrays: var firstNamesArray = ["John", "David", "Bob", "Sam", "Carol"]; var secondNamesArray = ["Mike", "Carol", "Adam", "David"]; var intersectionOfArray = firstNamesArray.filter(v => secondNamesArray.includes(v)); console.log("Intersection of two arrays:"); console.log(intersectionOfArray); Intersection of two arrays: [ 'David', 'Carol' ] How It Works The filter() method creates a new array with elements that pass the test. For each element in ...

Read More

map() array of object titles into a new array based on other property value JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 287 Views

Let's say, we have an array of objects like this − const arr = [{ country: "canada", count: 2 }, { country: "jamaica", count: 2 }, { country: "russia", count: 1 }, { country: "india", count: 3 }, { country: "spain", count: 2 }, { country: "portugal", count: 1 }, ...

Read More

Remove Seconds/ Milliseconds from Date and convert to ISO String?

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

In JavaScript, you can remove seconds and milliseconds from a Date object and convert it to an ISO string format. This is useful when you need precise time formatting without the seconds component. Getting the Current Date First, let's create a Date object with the current date and time: var currentDate = new Date(); console.log("The current date is: " + currentDate); The current date is: Sat Aug 01 2020 18:20:09 GMT+0530 (India Standard Time) Removing Seconds and Milliseconds Use the setSeconds() method to set both seconds and milliseconds to 0: ...

Read More

Convert 2D array to object using map or reduce in JavaScript

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

Converting a 2D array to an object is a common task in JavaScript. Let's say we have a two-dimensional array that contains data about people's ages. The data is given by the following 2D array: const data = [ ['Rahul', 23], ['Vikky', 27], ['Sanjay', 29], ['Jay', 19], ['Dinesh', 21], ['Sandeep', 45], ['Umesh', 32], ['Rohit', 28], ]; console.log(data); [ [ 'Rahul', 23 ], [ 'Vikky', 27 ], [ 'Sanjay', 29 ], ...

Read More

Retrieving object's entries in order with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 300 Views

In JavaScript, object properties don't maintain insertion order for numeric keys, but we can retrieve entries in sorted order using Object.keys() and sort() methods. The Problem Consider an object with numeric keys that aren't in sequential order: const subjectDetails = { 102: "Java", 105: "JavaScript", 104: "MongoDB", 101: "MySQL" }; console.log("Original object:"); console.log(subjectDetails); Original object: { '101': 'MySQL', '102': 'Java', '104': 'MongoDB', '105': 'JavaScript' } ...

Read More

Convert array of arrays to array of objects grouped together JavaScript

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

Let's say, we have a two-dimensional array that contains data about some colors and fruits like this: const data = [ ['orange', 'fruit'], ['red', 'color'], ['green', 'color'], ['orange', 'color'], ['banana', 'fruit'], ['blue', 'color'], ['lemon', 'fruit'], ['mango', 'fruit'], ['lemon', 'color'], ]; console.log("Original data:", data); Original data: [ [ 'orange', 'fruit' ], [ 'red', 'color' ], [ 'green', 'color' ], [ 'orange', 'color' ], [ 'banana', 'fruit' ], ...

Read More

Can someone explain to me what the plus sign is before the variables in JavaScript?

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

The plus (+) sign before a variable in JavaScript is the unary plus operator. It converts the value to a number, making it useful for type conversion from strings, booleans, or other data types to numeric values. Syntax +value How It Works The unary plus operator attempts to convert its operand to a number. It works similarly to Number() constructor but with shorter syntax. Example: String to Number Conversion var firstValue = "1000"; console.log("The data type of firstValue = " + typeof firstValue); var secondValue = 1000; console.log("The data type ...

Read More

Intersection of two arrays JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 751 Views

Finding the intersection of two arrays means finding elements that appear in both arrays, preserving the frequency of each element as it appears in both arrays. For example, if we have arr1 = [1, 2, 3, 1] and arr2 = [1, 3, 1], the intersection is [1, 3, 1] because element 1 appears twice in both arrays, and element 3 appears once in both. Problem Statement Given two arrays of numbers, we need to write a function that computes their intersection and returns an array containing the intersecting elements. Each element in the result should appear as ...

Read More

Find the middle element of an array using recursion JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 529 Views

We are required to write an array function, say findMiddle that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops. If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements. So, let's write the code for this function. As you've already guessed, we will be making use of recursion to find these elements. The code for the recursive function will be — How It ...

Read More
Showing 1401–1410 of 5,881 articles
« Prev 1 139 140 141 142 143 589 Next »
Advertisements