Javascript Articles

Page 119 of 534

How to group an array of objects by key in JavaScript

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

Suppose, we have an array of objects containing data about some cars like this: const arr = [ { 'make': 'audi', 'model': 'r8', 'year': '2012' }, { 'make': 'audi', 'model': 'rs5', 'year': '2013' }, { ...

Read More

Remove duplicates and map an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 657 Views

When working with arrays of objects, a common task is extracting unique values from a specific property. This tutorial shows how to remove duplicates and map an array to extract unique "name" values from objects. Problem Statement Suppose we have an array of objects like this: const arr = [ {id: 123, value: "value1", name: "Name1"}, {id: 124, value: "value2", name: "Name1"}, {id: 125, value: "value3", name: "Name2"}, {id: 126, value: "value4", name: "Name2"} ]; Note that some ...

Read More

Filter array of objects whose properties contains a value in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 978 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 740 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 318 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
Showing 1181–1190 of 5,340 articles
« Prev 1 117 118 119 120 121 534 Next »
Advertisements