Object Oriented Programming Articles

Page 38 of 589

Filter array of objects whose properties contains a value in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 984 Views

Filtering an array of objects based on property values is a common task in JavaScript. This technique allows you to search through all properties of objects to find matches based on a keyword. Sample Data Let's start with an array of objects containing user information: const arr = [{ name: 'Paul', country: 'Canada', }, { name: 'Lea', country: 'Italy', }, { name: 'John', country: 'Italy', }]; The Problem We need to filter objects where any ...

Read More

Check if user inputted string is in the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 524 Views

We need to write a JavaScript program that checks if a user-inputted string exists in a predefined array. The program should return true if the string is found, false otherwise. This is commonly needed when validating user input against a list of acceptable values, such as checking usernames, categories, or allowed options. Using Array.includes() Method The includes() method is the most straightforward way to check if an array contains a specific value. It returns a boolean result. Check ...

Read More

Count by unique key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 693 Views

When working with arrays of objects, you often need to count occurrences based on unique key values. This is particularly useful for data analysis, grouping records, or creating summary statistics. The Problem Given an array of objects with nested properties, we want to count how many times each unique user appears: const arr = [ { assigned_user: { name: 'Paul', ...

Read More

Merge arrays in column wise to another array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 741 Views

When working with multiple arrays, you often need to combine their elements column-wise to create structured data. This technique merges arrays by their corresponding indices to form an array of objects. Problem Statement Suppose we have three arrays of numbers like these: const code = [123, 456, 789]; const year = [2013, 2014, 2015]; const period = [3, 4, 5]; We need to merge these arrays column-wise to create an array of objects where each object contains the corresponding elements from all three arrays: const output = [ ...

Read More

Take in array of objects and convert the above JSON to a Tree-structure in JavaScript

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

In web development, you often need to convert flat arrays of hierarchical data into tree structures. This is common when working with organizational charts, file systems, or nested menus. Input Data Structure Suppose we have an array of objects representing parent-child relationships: const arr = [ { "parentIndex": '0', "childIndex": '3', "parent": "ROOT", "child": "root3" }, ...

Read More

How to process JavaScript nested array and display the order of numbers according to the level upto which they are nested?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

Processing nested arrays and displaying elements with proper indentation based on their nesting level is a common JavaScript task. This technique helps visualize the hierarchical structure of nested data. Problem Statement Given a nested array like this: const arr = [23, 6, [2, [6, 2, 1, 2], 2], 5, 2]; We need to display numbers with indentation showing their nesting level. Elements at deeper levels should have more indentation. Expected Output 23 6 2 6 2 ...

Read More

How to find inside an array of objects the object that holds the highest value in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 828 Views

Finding the object with the highest value in an array of objects is a common task in JavaScript. This example demonstrates how to find the student with the highest grade from an array of student objects. Sample Data Let's start with an array of student objects, where each student has a name and an array of grades: const arr = [ { name: "Student 1", grades: [ 65, 61, 67, 70 ] ...

Read More

How to merge two object arrays of different size by key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 866 Views

When working with object arrays of different sizes, we often need to merge them based on a common key. This is useful when combining data from different sources that share a common identifier. Suppose we have an object like this: const obj = { "part1": [{"id": 1, "a": 50}, {"id": 2, "a": 55}, {"id": 4, "a": 100}], "part2":[{"id": 1, "b": 40}, {"id": 3, "b": 45}, {"id": 4, "b": 110}] }; We need to merge part1 and part2 arrays to form a single array where objects with the same id ...

Read More

Deep Search JSON Object JavaScript

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

Deep searching a JSON object means recursively traversing through all nested objects and arrays to find elements that match specific criteria. This is useful when you need to locate objects buried deep within complex data structures. The Problem Suppose we have the following nested JSON object: const obj = { id: 1, title: 'hello world', child: { id: null, title: 'foobar', ...

Read More

Convert JSON to another JSON format with recursion JavaScript

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

Suppose, we have the following JSON object − const obj = { "context": { "device": { "localeCountryCode": "AX", "datetime": "3047-09-29T07:09:52.498Z" }, "currentLocation": { "country": "KM", ...

Read More
Showing 371–380 of 5,881 articles
« Prev 1 36 37 38 39 40 589 Next »
Advertisements