Horje
How to get Values from Specific Objects an Array in JavaScript ?

In JavaScript, an array is a data structure that can hold a collection of values, which can be of any data type, including numbers, strings, and objects. When an array contains objects, it is called an array of objects.

Using the forEach() method

The forEach() method is used to iterate through each element of an array and execute a provided function. It does not return a new array, but it’s helpful when you want to operate on each element without modifying the original array.

Example: The below example demonstrates how to get values from specific objects inside an array using the forEach method in JavaScript.

Javascript

const courses = [
    { name: 'C++', fees: 30000 },
    { name: 'Python', fees: 35000 },
    { name: 'JavaScript', fees: 40000 },
    { name: 'Java', fees: 45000 },
];
 
const cheapCourses = [];
 
courses.forEach((course) => {
    if (course.fees <= 35000) {
        cheapCourses.push(course.name);
        cheapCourses.push(course.fees);
    }
});
 
console.log(cheapCourses);

Output

[ 'C++', 30000, 'Python', 35000 ]

Using the map() method

The map() method is another array method that allows you to iterate over each element in an array and return a new array with the result of some operation on each element. You can use map() to create a new array of values from the properties of the objects in the original array.

Example: The below example demonstrates How to get values from specific objects inside an array in JavaScript Using the map() method.

Javascript

const course = [
    { name: "Java", fees: 25000 },
    { name: "C++", fees: 30000 },
    { name: "Python", fees: 35000 },
];
 
const courses =course
    .map((filObj) => filObj.name);
console.log(courses);

Output

[ 'Java', 'C++', 'Python' ]

Using the filter() method

The filter() method is an array method that allows you to iterate over each element in an array and return a new array with only the elements that meet some condition. You can use filter() to create a new array of objects that meet some condition based on their properties.

Example: The below example demonstrates How to get values from specific objects inside an array in JS Using the filter() method.

Javascript

const course = [
    { name: "Java", fees: 25000 },
    { name: "C++", fees: 30000 },
    { name: "Python", fees: 35000 },
];
 
const courses =course
    .filter((obj) => obj.fees >= 30000)
    .map((filObj) => filObj.name);
console.log(courses);

Output

[ 'C++', 'Python' ]



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Get Index of the Max Value in Array of Objects ? How to Get Index of the Max Value in Array of Objects ?
Convert Dictionary into an Array of objects in JavaScript Convert Dictionary into an Array of objects in JavaScript
How to Compare Two Date Strings in TypeScript ? How to Compare Two Date Strings in TypeScript ?
How to Disable Input in JavaScript ? How to Disable Input in JavaScript ?
How to Convert String to Date in TypeScript ? How to Convert String to Date in TypeScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13