英文:
Accessing a single value for specific key pair from object in a Javascript array
问题
以下是翻译好的代码部分:
let balanceInfo = students.map((student) => {
if (typeof(student) === Object){
let balance = student.balance;
return balance;
}
})
console.log(balanceInfo)
其中:
students是一个对象数组。.balance是对象中的一个键。
在遍历数组时,我想获取每个学生的余额信息。
英文:
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.
答案1
得分: 1
The problem is in typeof(student) === Object, you should write the object word in lowercase with quotes like "object".
let students = [
{
balance: 100,
},
{
balance: 200,
},
];
let balanceInfo = students.map((student) => {
if (typeof student === "object") {
let balance = student.balance;
return balance;
}
});
console.log(balanceInfo);
英文:
The problem is in typeof(student) === Object, you should write the object word in lowercase with quotes like "object".
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let students = [
{
balance: 100,
},
{
balance: 200,
},
];
let balanceInfo = students.map((student) => {
if (typeof student === "object") {
let balance = student.balance;
return balance;
}
});
console.log(balanceInfo);
<!-- end snippet -->
答案2
得分: 1
Your code is mostly correct, but there is a small issue with the typeof check. The typeof operator returns a string, so the comparison should be with the string 'object' rather than the Object constructor. Here's the corrected code:
let balanceInfo = students.map((student) => {
if (typeof student === 'object') {
let balance = student.balance;
return balance;
}
});
console.log(balanceInfo);
This code will iterate over the students array using map and extract the balance property from each object. The resulting balanceInfo array will contain all the balance values.
英文:
Your code is mostly correct, but there is a small issue with the typeof check. The typeof operator returns a string, so the comparison should be with the string 'object' rather than the Object constructor. Here's the corrected code:
let balanceInfo = students.map((student) => {
if (typeof student === 'object') {
let balance = student.balance;
return balance;
}
});
console.log(balanceInfo);
This code will iterate over the students array using map and extract the balance property from each object. The resulting balanceInfo array will contain all the balance values.
答案3
得分: 0
从你的.map()中看起来,你想要所有学生的余额。
如果你只想要打印余额,你可以在迭代students数组时使用控制台
students.forEach((student) => {
console.log(student.balance)
})
英文:
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 array
students.forEach((student) => {
console.log(student.balance)
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论