简体   繁体   English

将逗号分隔的值显示为列表项:

[英]Display comma separated values as list items:

This is a continuation from this question: How to display comma delimited JSON value as a list? 这是该问题的延续: 如何将逗号分隔的JSON值显示为列表?

I have this array: 我有这个数组:

var ARTISTS: Artist[] = [
  {
     "name": "Barot Bellingham",
     "shortname": "Barot_Bellingham",
     "reknown": "Royal Academy of Painting and Sculpture",
     "bio": "Some bio here...",
     "friends": [
       "james",
       "harry",
       "bob"
    ]
  },
  // etc...
]

And I need to display "friends" as an ordered list. 我需要将“朋友”显示为有序列表。 I'm doing so like this: 我这样做是这样的:

<li *ngFor="let friend of artist.friends">{{friend}}</li>

This works perfectly, but , I now need to restructure my data so that there is no nested array: 这完美地工作了, 但是 ,我现在需要重组我的数据,以便没有嵌套的数组:

var ARTISTS: Artist[] = [
  {
     "name": "Barot Bellingham",
     "shortname": "Barot_Bellingham",
     "reknown": "Royal Academy of Painting and Sculpture",
     "bio": "Some bio here...",
     "friends": "James, Harry, Bob"
  }

How can I continue to display each friend as separate list items, such as: 如何继续将每个朋友显示为单独的列表项,例如:

<ol>
  <li>James</li>
  <li>Harry</li>
  <li>Bob</li>
</ol>

Thank you! 谢谢!

Simply use the toString() method to display the list of a string separated by commas. 只需使用toString()方法即可显示以逗号分隔的字符串列表。

 var friends = ['a', 'b', 'c']; console.log(friends.toString()); 

So in your case you could just change this: 因此,在您的情况下,您可以更改此设置:

<li *ngFor="let friend of artist.friends">{{friend}}</li>

to this for example: 例如:

<div>{{artist.friends.toString()}}</div>

If you want to change the separator, or add a space after the commas, you can use the join() method of arrays too: 如果要更改分隔符或在逗号后添加空格,则也可以使用数组的join()方法:

 var friends = ['a', 'b', 'c']; console.log(friends.join(', ')); 

Instead if now you have in your model the list as a string separated by comma, use the ng-repeat recreating an array from the string in this way: 相反,如果现在您的模型中列表是用逗号分隔的字符串,请使用ng-repeat通过这种方式从字符串ng-repeat创建数组:

<li *ngFor="let friend of artist.friends.split(', ')">{{friend}}</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM