Object Oriented Programming Articles

Page 143 of 589

Recursively flat an object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

We are required to write a function that flattens nested objects by converting deeply nested properties into dot-notation keys. This is useful for data transformation and API processing. If the input object is: const input = { a: 0, b: {x: {y: 1, z: 2}}, c: 3 }; Then the output of the function should be: const output = { a: 0, 'b.x.y': 1, 'b.x.z': 2, c: 3 } Recursive ...

Read More

Add object to array in JavaScript if name does not already exist?

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

To add objects to an array only if they don't already exist, you can use push() combined with find() to check for duplicates. This prevents adding duplicate entries based on specific criteria. Basic Example Here's how to add objects to an array if the name property doesn't already exist: var details = [{name: "John"}, {name: "David"}]; var addObject = ["Mike", "Sam", "John"]; // "John" already exists addObject.forEach(obj1 => { if (!details.find(obj2 => obj2.name === obj1)) { details.push({name: obj1}); } ...

Read More

How do I write a function that takes an array of values and returns an object JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 624 Views

Let's say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types. For example − // if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; // then the output Map should be: Map(5) { 'string' => [ 'class', 'name' ], 'number' => [ 2, 6 ], 'object' => [ [ 7, 8, 9 ], { name: 'Michael' ...

Read More

How to ignore using variable name as a literal while using push() in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 292 Views

To avoid using a variable name as a literal string when pushing objects to an array, use square brackets [variableName] for computed property names. This allows the variable's value to become the property key. The Problem with Literal Property Names When you use name: value in an object, "name" becomes a literal string property, not the variable's value: var name = "David"; var data = []; // This creates a property literally named "name" data.push({ name: "This will always be 'name' property" }); console.log(data); [ { name: 'This will always be 'name' ...

Read More

Split one-dimensional array into two-dimensional array JavaScript

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

We are required to write a function that takes in a one-dimensional array as the first argument and a number n as the second argument and we have to make n subarrays inside of the parent array (if possible) and divide elements into them accordingly. If the array contains 9 elements and we asked to make 4 subarrays, then dividing 2 elements in each subarray creates 5 subarrays and 3 in each creates 3, so in such cases we have to fallback to nearest lowest level (3 in ...

Read More

Make first letter of a string uppercase in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 506 Views

To make first letter of a string uppercase, use toUpperCase() in JavaScript. With that, we will use charAt(0) since we need to only capitalize the 1st letter. Syntax string.charAt(0).toUpperCase() + string.slice(1) Example function replaceWithTheCapitalLetter(values){ return values.charAt(0).toUpperCase() + values.slice(1); } var word = "javascript" console.log(replaceWithTheCapitalLetter(word)); Javascript Multiple Examples function capitalizeFirst(str) { return str.charAt(0).toUpperCase() + str.slice(1); } console.log(capitalizeFirst("hello world")); console.log(capitalizeFirst("programming")); console.log(capitalizeFirst("a")); console.log(capitalizeFirst("")); Hello world Programming A How It Works The method breaks ...

Read More

Sort array based on another array in JavaScript

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

We often need to sort an array based on the order specified in another array. This technique is useful when you want certain elements to appear first while maintaining the original order of remaining elements. For example, we have an original array and want elements from a sortOrder array to appear at the beginning: const originalArray = ['Apple', 'Cat', 'Fan', 'Goat', 'Van', 'Zebra']; const sortOrder = ['Zebra', 'Van']; Using Custom Sort Function We can create a custom comparator function that prioritizes elements present in the sortOrder array: const originalArray = ['Apple', 'Cat', ...

Read More

How to divide an unknown integer into a given number of even parts using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 461 Views

When dividing an integer into equal parts, you may need to distribute the remainder across some parts to ensure all values are used. This can be achieved using the modular operator and array manipulation. How It Works The algorithm divides the integer by the number of parts to get a base value. If there's a remainder, it distributes the extra values by adding 1 to some parts. Example function divideInteger(value, parts) { var baseValue; var remainder = value % parts; ...

Read More

Filter an object based on an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 835 Views

Let's say we have an array and an object like this: const arr = ['a', 'd', 'f']; const obj = { "a": 5, "b": 8, "c": 4, "d": 1, "e": 9, "f": 2, "g": 7 }; We are required to write a function that takes in the object and the array and filter away all the object properties that are not an element of the array. So, the output should only contain 3 properties, namely: "a", "d" and "f". Method 1: ...

Read More

In JavaScript, can be use a new line in console.log?

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

Yes, you can use newlines in console.log() using the escape character. This creates line breaks in console output. Basic Newline Usage console.log("First lineSecond lineThird line"); First line Second line Third line Multiple Ways to Add Newlines There are several approaches to include newlines in console output: // Method 1: Using escape character console.log("HelloWorld"); // Method 2: Multiple console.log() calls console.log("Hello"); console.log("World"); // Method 3: Template literals with actual line breaks console.log(`Hello World`); // Method 4: Combining text with newlines console.log("Name: JohnAge: 25City: New York"); ...

Read More
Showing 1421–1430 of 5,881 articles
« Prev 1 141 142 143 144 145 589 Next »
Advertisements