I have an array of objects with student information, I want to get a single value that is attached to a particular key using array.map
Here’s the code
let balanceInfo = students.map((student) => {
if (typeof(student) === Object){
let balance = student.balance;
return balance;
}
})
console.log(balanceInfo)
Where:
students
is an array of objects
.balance
is a key in the object
When the array is being traversed, I want to get the balance details of each student.
3
Answers
From your
.map()
it looks like you want the balance of all the students.If you want only balance printed in, you can put console while iterating the
students
arrayThe problem is in
typeof(student) === Object
, you should write theobject
word in lowercase with quotes like"object"
.Your code is mostly correct, but there is a small issue with the
typeof
check. Thetypeof
operator returns a string, so the comparison should be with the string'object'
rather than theObject
constructor. Here’s the corrected code:This code will iterate over the
students
array usingmap
and extract thebalance
property from each object. The resultingbalanceInfo
array will contain all the balance values.