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 }
]