如何将逗号分隔的JSON值显示为列表? [英] How to display comma delimited JSON value as a list?

查看:604
本文介绍了如何将逗号分隔的JSON值显示为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个像这样的数组:

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.

谢谢!

P.S.我正在使用Angular 2.

P.S. I'm using Angular 2.

推荐答案

您可以使用

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

这篇关于如何将逗号分隔的JSON值显示为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆