如何访问分配给另一个对象的属性的数组中的对象的属性?

时间:2019-06-02 07:57:31

标签: javascript arrays node.js reactjs mongoose

如何访问subjects数组内部的name属性?

数据库是mongodb。 不能更改课程模型。

课程模型:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const SubjectSchema = new Schema({

name : {
    type : String
},
description : {
    type : String
},
amount : {
    type : Number
},
});

  //course schema
   const CourseSchema = new Schema({

name: {
    type : String
},
code : {
    type: String
},
passMark : {
    type : Number
},
lectureInCharge : {
    type : String
},
subjects : {
    type : [SubjectSchema]
}

});

//creating model
const Course = mongoose.model('course', CourseSchema);

   module.exports = Course;

我要访问课程主题详细信息的代码? 我想显示课程详细信息以及位于课程内部的主题详细信息 课程详情。但是主题位于数组内,该数组分配给课程对象的subject属性。

这是一个React界面。

 const courses = this.state.courses;
    const updatedCourse = courses.map(function (data,  index) {
        return (
            <div key={index}>
                <p> Name : {data.name}</p>
                <p> Code : {data.code}</p>
                <p> Pass Mark : {data.passMark}</p>
                <p> lecture in charge : {data.lectureInCharge}</p>
                <p> Subjects : </p>
                 //Here i want aceess the ame property of the inside the 
                  subjects array?
                <p> Subject name : {data.subjects.name}</p>
            </div>
        )
    });

从数据库中检索的的json如下所示。 包括以了解数据库的外观。

[
{
    "_id": "5cf348111b0ffd3bc02304b8",
    "name": "Software Engineering",
    "code": "SE2019",
    "passMark": 75,
    "lectureInCharge": "Jhon Smith",
    "subjects": [
        {
            "_id": "5cf348111b0ffd3bc02304b9",
            "name": "Computer Architecture",
            "description": "PC Architecture x86 and x64",
            "amount": 2500
        }
    ],
    "__v": 0
},
{
    "_id": "5cf358991b0ffd3bc02304ba",
    "name": "Computer Networking",
    "code": "CN2019",
    "passMark": 75,
    "lectureInCharge": "Jimmy Perera",
    "subjects": [
        {
            "_id": "5cf358991b0ffd3bc02304bc",
            "name": "Wireless Communications",
            "description": "Introduction to Wireless Communications",
            "amount": 5000
        },
        {
            "_id": "5cf358991b0ffd3bc02304bb",
            "name": "Network Technology Project",
            "description": "Introduction to Network Technology Project",
            "amount": 7000
        }
    ],
    "__v": 0
},
{
    "_id": "5cf3593d1b0ffd3bc02304c0",
    "name": "IM",
    "code": "IM2019",
    "passMark": 75,
    "lectureInCharge": "IMIM Jimmy Perera",
    "subjects": [
        {
            "_id": "5cf3593d1b0ffd3bc02304c2",
            "name": "IM Wireless Communications",
            "description": " IM Introduction to Wireless Communications",
            "amount": 3000
        },
        {
            "_id": "5cf3593d1b0ffd3bc02304c1",
            "name": "IM Network Technology Project",
            "description": "IM Introduction to Network Technology Project",
            "amount": 7700
        }
    ],
    "__v": 0
}
]

1 个答案:

答案 0 :(得分:2)

由于它是一个数组,因此需要一个内部循环(可能是另一个map):

const courses = this.state.courses;
const updatedCourse = courses.map(function (data,  index) {
    return (
        <div key={index}>
            <p> Name : {data.name}</p>
            <p> Code : {data.code}</p>
            <p> Pass Mark : {data.passMark}</p>
            <p> lecture in charge : {data.lectureInCharge}</p>
            <p> Subjects : </p>
            {data.subjects.map(({name}, i) => (         // <===
                <p key={i}> Subject name : {name}</p>   // <===
            )}
        </div>
    );
});