How to display comma delimited JSON value as a list?

廉价感情. 提交于 2019-12-24 09:29:00

问题


Say I have an array like this:

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

Is it possible to display values for the key “friends” as an unordered list where each friend would be it’s own list item, e.g.:

    <ul>
      <li>James</li>
      <li>Harry</li>
      <li>Bob</li>
    </ul>

I do realize the "friends" would be better stored as a nested array in order to display as a list, but I'm looking into this as a potential workaround for another issue.

Thank you!

P.S. I'm using Angular 2.


回答1:


You can use String.prototype.split() to get an array of friends.

var arrayOfFriends = Artist[0].friends.split(", ");
// arrayOfFriends = ['James', 'Harry', 'Bob']

From there loop over the array and create your list



来源:https://stackoverflow.com/questions/44729743/how-to-display-comma-delimited-json-value-as-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!