Accessing a single value for specific key pair from object in a Javascript array

huangapple go评论71阅读模式
英文:

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) =&gt; {
    if (typeof student === &quot;object&quot;) {
        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 &#39;object&#39; rather than the Object constructor. Here's the corrected code:

let balanceInfo = students.map((student) =&gt; {
    if (typeof student === &#39;object&#39;) {
        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) =&gt; {
    console.log(student.balance)
}) 

huangapple
  • 本文由 发表于 2023年7月4日 20:23:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定