Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

javascript - 返回数组内对象的值(Return the values of an object inside of array)

I am trying to return a value of an objects that are inside of an array.

(我试图返回数组内部对象的值。)

 const bugSchema = new Schema({ title: { type: String, required: true }, comments:[ { user:{ type: String, required: true }, content: { type: String, required: true } } ] }); 

I have tried the following function but it does not return anything.

(我尝试了以下功能,但未返回任何内容。)

  <% for (bug of bugs) { %> <ion-card> <ion-card-header> <ion-card-title><%= bug.comments.filter(function (value) { value.content }) %></ion-card-title> </ion-card-header> <ion-card-content> </ion-card-content> </ion-card> <% } %> 

However I can return an array but it returns the entire objects rather than value of each one.

(但是我可以返回一个数组,但它返回整个对象,而不是每个对象的值。)

  <% for (bug of bugs) { %> <ion-card> <ion-card-header> <ion-card-title><%= bug.comments %></ion-card-title> </ion-card-header> <ion-card-content> </ion-card-content> </ion-card> <% } %> 

The screenshot below shows how the object looks like

(下面的屏幕截图显示了对象的外观)

在此处输入图片说明

What I am expecting to achieve is to create a ion-card populated with comment's content.

(我期望实现的目标是创建一个包含评论内容的离子卡 。)

  ask by HalfMartianHalfHuman translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this shall work:

(我认为这应该起作用:)

<% for (bug of bugs) { %>
  <% for (comment of bug.comments) { %>
    <ion-card>
      <ion-card-header>
        <ion-card-title>
          <%= comment.user %>
        </ion-card-title>
      </ion-card-header>
      <ion-card-content>
        <%= comment.content %>
      </ion-card-content>
    </ion-card>
  <% } %>
<% } %>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...