Get only specific values in an array of objects in JavaScript?

JavascriptWeb DevelopmentObject Oriented Programming

Let’s say the following is our array of objects −

var details = [{
   studentName: "John",
   studentMarks: 92
},
{
   studentName: "David",
   studentMarks: 89
},
{
   studentName: "Mike",
   studentMarks: 98
},
];

To get only specific values in an array of objects in JavaScript, use the concept of filter().

Example

var details = [{
   studentName: "John",
   studentMarks: 92
},
{
   studentName: "David",
   studentMarks: 89
},
{
   studentName: "Mike",
   studentMarks: 98
},
];
var specificValuesFromArray = details.filter(obj => obj.studentMarks ===
92 || obj.studentMarks === 98);
console.log(specificValuesFromArray)

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo177.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo177.js
[
   { studentName: 'John', studentMarks: 92 },
   { studentName: 'Mike', studentMarks: 98 }
]
raja
Published on 12-Sep-2020 08:55:01
Advertisements